pthread_mutexattr_getpshared()--Get Process Shared Attribute from Mutex Attributes Object


  Syntax:
 #include <pthread.h>
 int pthread_mutexattr_getpshared(const pthread_mutexattr_t *attr, int *pshared);  
  Service Program Name: QP0WPTHR

  Default Public Authority: *USE

  Threadsafe: Yes

  Signal Safe: Yes

The pthread_mutexattr_getpshared() function retrieves the current setting of the process shared attribute from 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 in storage 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.


Authorities and Locks

None.


Parameters

attr
(Input) Address of the variable that contains the mutex attributes object
pshared
(Output) Address of the variable to contain the pshared attribute result

Return Value

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

Error Conditions

If pthread_mutexattr_getpshared() 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

Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.

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

void showPshared(int pshared) {
  printf("The pshared attribute 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);

  printf("Check pshared attribute\n");
  rc = pthread_mutexattr_getpshared(&mta, &pshared);
  checkResults("pthread_mutexattr_getpshared()\n", rc);
  showPshared(pshared);
 
  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
Destroy mutex attribute
Main completed

API introduced: V4R3

[ Back to top | Pthread APIs | APIs by category ]