Name

pthread_mutexattr_init, pthread_mutexattr_destroy, pthread_mutexattr_settype
pthread_mutexattr_gettype, pthread_mutexattr_getpshared, pthread_mutexattr_setpshared

- mutex attribute operations

Library

libc_r.lib libpthread.lib libthr.lib

Synopsis

  #include <pthread.h>
  int pthread_mutexattr_init (pthread_mutexattr_t *attr);
  int pthread_mutexattr_destroy (pthread_mutexattr_t *attr);
  int pthread_mutexattr_settype (pthread_mutexattr_t *attr, int type);
  int pthread_mutexattr_gettype (pthread_mutexattr_t *attr, int *type);
  int pthread_mutexattr_getpshared (pthread_mutexattr_t *attr, int *pshared);
  int pthread_mutexattr_setpshared (pthread_mutexattr_t *attr, int pshared);

Return values

If successful, these functions return 0. Otherwise, an error number is returned to indicate the error.

Detailed description

Mutex attributes are used to specify parameters to pthread_mutex_init. One attribute object can be used in multiple calls to pthread_mutex_init, with or without modifications between calls.

The pthread_mutexattr_init function initializes attr with all the default mutex attributes.

The pthread_mutexattr_destroy function destroys attr.

The pthread_mutexattr_set* functions set the attribute that corresponds to each function name.

The pthread_mutexattr_get* functions copy the value of the attribute that corresponds to each function name to the location pointed to by the second function parameter.


Examples

pthread_mutexattr_t mta;
 int rc;
 /* Initialize a mutex attributes object */
 if((rc=pthread_mutexattr_init(&mta)) != 0)
 {
  fprintf(stderr,"Cannot initialize mutex attributes object\n");
  return -1;
 }
 /* Destroy the mutex attributes object */
 if(pthread_mutexattr_destroy(&mta) != 0)
 {
  fprintf(stderr,"Error at pthread_mutexattr_destroy(), rc=%d\n", rc);
  return -1;
 }

         

 pthread_mutexattr_t mta;
 int pshared;
 /* Initialize a mutex attributes object */
 if(pthread_mutexattr_init(&mta) != 0)
 {
  perror("Error at pthread_mutexattr_init()\n");
  return -1;
 }
  /* The default ’pshared’ attribute is PTHREAD_PROCESS_PRIVATE  */
 if(pthread_mutexattr_getpshared(&mta, &pshared) != 0)
 {
  fprintf(stderr,"Error obtaining the attribute process-shared\n");
  return -1;
 }
if (pshared != PTHREAD_PROCESS_PRIVATE)
     printf (" pshared is NOT PTHREAD_PROCESS_PRIVATE\n");

         

 pthread_mutexattr_t mta;
 int type;
 /* Initialize a mutex attributes object */
 if(pthread_mutexattr_init(&mta) != 0)
 {
  perror("Error at pthread_mutexattr_init()\n");
  return -1;
 }
  /* The default ’type’ attribute  is PTHREAD_MUTEX_DEFAULT  */
 if(pthread_mutexattr_gettype(&mta, &type) != 0)
 {
  fprintf(stderr,"pthread_mutexattr_gettype(): Error obtaining the attribute ’type’\n");
  return -1;
 }
 if(type != PTHREAD_MUTEX_DEFAULT)
 {
  printf("FAILED: Incorrect default mutexattr ’type’ value: %d\n", type);
  return -1;
 }

         

pthread_mutexattr_t mta;
 int rc;
 /* Initialize a mutex attributes object */
 if((rc=pthread_mutexattr_init(&mta)) != 0)
 {
  fprintf(stderr,"Cannot initialize mutex attributes object\n");
  return -1;
 }
 /* Destroy the mutex attributes object */
 if(pthread_mutexattr_destroy(&mta) != 0)
 {
  fprintf(stderr,"Error at pthread_mutexattr_destroy(), rc=%d\n", rc);
  return -1;
 }

         

pthread_mutexattr_t mta;
 int ret;
 /* Initialize a mutex attributes object */
 if(pthread_mutexattr_init(&mta) != 0)
 {
  perror("Error at pthread_mutexattr_init()\n");
  return -1;
 }
 /* Set the ’pshared’ attribute to PTHREAD_PROCESS_PRIVATE */
 if((ret=pthread_mutexattr_setpshared(&mta, PTHREAD_PROCESS_PRIVATE)) != 0)
 {
  printf("FAILED: Cannot set pshared attribute to PTHREAD_PROCESS_PRIVATE. Error: %d\n", ret);
  return -1;
 }

         

 pthread_mutexattr_t mta;
 int type;
 /* Initialize a mutex attributes object */
 if(pthread_mutexattr_init(&mta) != 0)
 {
  perror("Error at pthread_mutexattr_init()\n");
  return -1;
 }
 if(pthread_mutexattr_gettype(&mta, &type) != 0)
 {
  printf("Error getting the attribute ’type’\n");
  return -1;
 }
 if(type != PTHREAD_MUTEX_DEFAULT)
 {
  printf("FAILED: Default value of the ’type’ attribute is not PTHREAD_MUTEX_DEFAULT \n");
  return -1; 
 }
 if(pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_NORMAL) != 0)
 {
  printf("FAILED: Error setting the attribute ’type’\n");
  return -1;
 }

         

Limitation

The pthread_mutexattr_setpshared supports only PTHREAD_PROCESS_PRIVATE


Errors

The pthread_mutexattr_init function will fail if:
[EINVAL]
  Invalid value for attr.

The pthread_mutexattr_destroy function will fail if:

[EINVAL]
  Invalid value for attr.

The pthread_mutexattr_settype function will fail if:

[EINVAL]
  Invalid value for attr, or invalid value for type.

The pthread_mutexattr_gettype function will fail if:

[EINVAL]
  Invalid value for attr.

The pthread_mutexattr_setpshared function will fail if:

[EINVAL]
  Invalid value for attr, or invalid value for type.

The pthread_mutexattr_getpshared function will fail if:

[EINVAL]
  Invalid value for attr.


See also

pthread_mutex_init

Feedback

For additional information or queries on this page send feedback

© 2005-2007 Nokia

Top