You can think of an attributes object as a private structure. You read or write the "members" of the structure by calling special functions, rather than by accessing public member names. For example, you read the
pthread_attr_getstacksize, or write it by calling pthread_attr_setstacksize.In a simple implementation of Pthreads the type pthread_attr_t
Threads, mutexes, and condition variables each have their own special attributes object type. Respectively, the types are pthread_attr_t, pthread_ mutexattr_t, and pthread_condattr_t.
5.2.1 Mutex attributes
pthread_mutexattr_t attr;
int pthread_mutexattr_init (pthread_mutexattr_t *attr);
int pthread_mutexattr_destroy (
pthread_mutexattr_t *attr);
#ifdef _POSIX_THREAD_PROCESS_SHARED
int pthread_mutexattr_getpshared (
pthread_mutexattr_t *attr, int *pshared); int pthread_mutexattr_setpshared (
pthread_mutexattr_t *attr, int pshared);
#endif
Pthreads defines the following attributes for mutex creation:
You initialize a mutex attributes object by calling pthread_mutexattr_init
pthread_mutexattr_t, as in mutex_ attr.c, shown next. You use that attributes object by passing its address to pthread_mutex_init instead of the NULL value we've been using so far.If your system provides the _POSIX_THREAD_PROCESS_SHARED option, then it supports the
pthread_mutexattr_setpshared. If you set the pthread_ mutex_t) is initialized. The default value for this attribute is PTHREAD_PROCESS_PRIVATE.The mutex_attr.c program shows how to set a mutex attributes object to create a mutex using the pshared
■ mutex_attr.c
1 #include
2 #include "errors.h"
3
4 pthread_mutex_t mutex;
5
6 int main (int argc, char *argv[])
7 {
8 pthread_mutexattr_t mutex_attr;
9 int status;
10
11 status = pthread_mutexattr_init (&mutex_attr);
12 if (status != 0)
13 err_abort (status, "Create attr");
14 #ifdef _POSIX_THREAD_PROCESS_SHARED
15 status = pthread_mutexattr_setpshared (
16 &mutex_attr, PTHREAD_PROCESS_PRIVATE);
17 if (status != 0)
18 err_abort (status, "Set pshared");
19 #endif
20 status = pthread_mutex_init (&mutex, &mutex_attr);
21 if (status != 0)
22 err_abort (status, "Init mutex");
23 return 0;
24 }
5.2.2 Condition variable attributes
pthread_condattr_t attr;
int pthread_condattr_init (pthread_condattr_t *attr);
int pthread_condattr_destroy (
pthread_condattr_t *attr);
#ifdef _POSIX_THREAD_PROCESS_SHARED
int pthread_condattr_getpshared (
pthread_condattr_t *attr, int *pshared);
int pthread_condattr_setpshared (