[Prev] [Contents] [Next]

pthread_mutexattr_setpshared()--Set Process Shared Attribute in Mutex Attributes Object

Syntax

#include <pthread.h>
int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr,
                                 int pshared);
Threadsafe: Yes
Signal Safe: Yes

The pthread_mutexattr_setpshared() function sets the current pshared attribute for the mutex attributes object. The process shared attribute indicates whether the mutex that is created using the mutex attributes object can be shared between threads in separate processes (PTHREAD_PROCESS_SHARED) or shared between threads within the same process (PTHREAD_PROCESS_PRIVATE).

Even if the mutex is in storage that is accessible from two separate processes, it cannot be used from both processes unless the process shared attribute is PTHREAD_PROCESS_SHARED.

The default pshared attribute for mutex attributes objects is PTHREAD_PROCESS_PRIVATE.

Parameters

attr
(Input) Address of the variable containing the mutex attributes object
pshared
(Input) One of PTHREAD_PROCESS_SHARED or PTHREAD_PROCESS_PRIVATE

Authorities and Locks

None.

Return Value

0
pthread_mutexattr_setpshared() was successful.
value
pthread_mutexattr_setpshared() was not successful. value is set to indicate the error condition.

Error Conditions

If pthread_mutexattr_setpshared() was not successful, the error condition returned usually indicates one of the following errors. Under some conditions, the value returned could indicate an error other than those listed here.

[EINVAL]
The value specified for the argument is not correct.

Related Information

Example

#include <pthread.h>
#include <stdio.h>
#include "check.h"

void showPshared(pthread_mutexattr_t *mta) {
  int           rc;
  int           pshared;
  
  printf("Check pshared attribute\n");
  rc = pthread_mutexattr_getpshared(mta, &pshared);
  checkResults("pthread_mutexattr_getpshared()\n", rc);
  
  printf("The pshared attributed is: ");
  switch (pshared) {
  case PTHREAD_PROCESS_PRIVATE:
    printf("PTHREAD_PROCESS_PRIVATE\n");
    break;
  case PTHREAD_PROCESS_SHARED:
    printf("PTHREAD_PROCESS_SHARED\n");
    break;
  default : 
    printf("! pshared Error !\n");
    exit(1);  
  }
  return;
}

int main(int argc, char **argv)
{
  int                   rc=0;
  pthread_mutexattr_t   mta;
  int                   pshared=0;

  printf("Entering testcase\n");

  printf("Create a default mutex attribute\n");
  rc = pthread_mutexattr_init(&mta);
  checkResults("pthread_mutexattr_init()\n", rc);
  showPshared(&mta);

  printf("Change pshared attribute\n");
  rc = pthread_mutexattr_setpshared(&mta, PTHREAD_PROCESS_SHARED);
  checkResults("pthread_mutexattr_setpshared()\n", rc);  
  showPshared(&mta);
  
  printf("Destroy mutex attribute\n");
  rc = pthread_mutexattr_destroy(&mta);
  checkResults("pthread_mutexattr_destroy()\n", rc);
  
  printf("Main completed\n");
  return 0;
}
Output

Entering testcase
Create a default mutex attribute
Check pshared attribute
The pshared attribute is: PTHREAD_PROCESS_PRIVATE
Change pshared attribute
The pshared attribute is: PTHREAD_PROCESS_SHARED
Destroy mutex attribute
Main completed


[Prev] [Contents] [Next]
Copyright © 1998, IBM Corporation. All rights reserved.
Comments? Contact
rchthrds@us.ibm.com