pthread_mutexattr_setkind_np()--Set Mutex Kind Attribute


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

  Default Public Authority: *USE

  Threadsafe: Yes

  Signal Safe: Yes

The pthread_mutexattr_setkind_np() function sets the kind attribute in the mutex attributes object specified by attr. The mutex kind attribute is used to create mutexes with different behaviors.

The kind set may be one of PTHREAD_MUTEX_NONRECURSIVE_NP or PTHREAD_MUTEX_RECURSIVE_NP.

A recursive mutex can be locked repeatedly by the owner. The mutex does not become unlocked until the owner has called pthread_mutex_unlock() for each successful lock request that it has outstanding on the mutes. The maximum number of recursive locks by the owning thread is 32,767.

Note: This function is not portable


Authorities and Locks

None.


Parameters

attr
(Input) Address of the mutex attributes object
kind
(Input) Variable containing the kind attribute.

Return Value

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

Error Conditions

If pthread_mutexattr_setkind_np() 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 showKind(pthread_mutexattr_t *mta) {
  int           rc;
  int           kind;

  printf("Check kind attribute\n");
  rc = pthread_mutexattr_getkind_np(mta, &kind);
  checkResults("pthread_mutexattr_getpshared()\n", rc);

  printf("The pshared attributed is: ");
  switch (kind) {
  case PTHREAD_MUTEX_NONRECURSIVE_NP:
    printf("PTHREAD_MUTEX_NONRECURSIVE_NP\n");
    break;
  case PTHREAD_MUTEX_RECURSIVE_NP:
    printf("PTHREAD_MUTEX_RECURSIVE_NP\n");
    break;
  default :
    printf("! kind Error kind=%d !\n", kind);
    exit(1);
  }
  return;
}

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

  printf("Enter Testcase - %s\n", argv[0]);

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

  printf("Change mutex kind attribute\n");
  rc = pthread_mutexattr_setkind_np(&mta, PTHREAD_MUTEX_RECURSIVE_NP);
  checkResults("pthread_mutexattr_setkind()\n", rc);
  showKind(&mta);

  printf("Destroy mutex attribute\n");
  rc = pthread_mutexattr_destroy(&mta);
  checkResults("pthread_mutexattr_destroy()\n", rc);

  printf("Main completed\n");
  return 0;
}

Output:

Enter Testcase - QP0WTEST/TPMTXAKN0
Create a default mutex attribute
Check kind attribute
The pshared attributed is:
PTHREAD_MUTEX_NONRECURSIVE_NP
Change mutex kind attribute
Check kind attribute
The pshared attributed is:
PTHREAD_MUTEX_RECURSIVE_NP
Destroy mutex attribute
Main completed

API introduced: V4R3

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