forked from hailinzeng/Programming-POSIX-Threads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutex_attr.c
35 lines (32 loc) · 1.03 KB
/
mutex_attr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
* mutex_attr.c
*
* Create a mutex using a non-default attributes object,
* mutex_attr. If the implementation supports the pshared
* attribute, the mutex is created "process private" so that it
* could be used to synchronize between threads in separate
* address spaces. (Note that, to create a "process shared"
* mutex, the pthread_mutex_t itself must be placed in shared
* memory that is accessible to all threads using the mutex.)
*/
#include <pthread.h>
#include "errors.h"
pthread_mutex_t mutex;
int main (int argc, char *argv[])
{
pthread_mutexattr_t mutex_attr;
int status;
status = pthread_mutexattr_init (&mutex_attr);
if (status != 0)
err_abort (status, "Create attr");
#ifdef _POSIX_THREAD_PROCESS_SHARED
status = pthread_mutexattr_setpshared (
&mutex_attr, PTHREAD_PROCESS_PRIVATE);
if (status != 0)
err_abort (status, "Set pshared");
#endif
status = pthread_mutex_init (&mutex, &mutex_attr);
if (status != 0)
err_abort (status, "Init mutex");
return 0;
}