1pthread_mutex_init(3C) Standard C Library Functions pthread_mutex_init(3C)
2
3
4
6 pthread_mutex_init, pthread_mutex_destroy - initialize or destroy a
7 mutex
8
10 cc -mt [ flag... ] file... -lpthread [ library... ]
11 #include <pthread.h>
12
13 int pthread_mutex_init(pthread_mutex_t *restrict mutex,
14 const pthread_mutexattr_t *restrict attr);
15
16
17 int pthread_mutex_destroy(pthread_mutex_t *mutex);
18
19
20 pthread_mutex_t mutex= PTHREAD_MUTEX_INITIALIZER;
21
22
24 The pthread_mutex_init() function initializes the mutex referenced by
25 mutex with attributes specified by attr. If attr is NULL, the default
26 mutex attributes are used; the effect is the same as passing the
27 address of a default mutex attributes object. Upon successful initial‐
28 ization, the state of the mutex becomes initialized and unlocked.
29
30
31 Except for robust mutexes, attempting to initialize an already initial‐
32 ized mutex results in undefined behavior.
33
34
35 The pthread_mutex_destroy() function destroys the mutex object refer‐
36 enced by mutex; the mutex object becomes, in effect, uninitialized. A
37 destroyed mutex object can be re-initialized using
38 pthread_mutex_init(); the results of otherwise referencing the object
39 after it has been destroyed are undefined.
40
41
42 It is safe to destroy an initialized mutex that is unlocked. Attempting
43 to destroy a locked mutex results in undefined behavior.
44
45
46 In cases where default mutex attributes are appropriate, the macro
47 PTHREAD_MUTEX_INITIALIZER can be used to initialize mutexes that are
48 statically allocated. The effect is equivalent to dynamic initializa‐
49 tion by a call to pthread_mutex_init() with parameter attr specified as
50 NULL, except that no error checks are performed.
51
53 If successful, the pthread_mutex_init() and pthread_mutex_destroy()
54 functions return 0. Otherwise, an error number is returned to indicate
55 the error.
56
58 The pthread_mutex_init() function will fail if:
59
60 EAGAIN The system lacked the necessary resources (other than memory)
61 to initialize another mutex.
62
63
64 EBUSY An attempt was detected to re-initialize a robust mutex pre‐
65 viously initialized but not yet destroyed. See pthread_mutex‐
66 attr_setrobust(3C).
67
68
69 EINVAL An attempt was detected to re-initialize a robust mutex pre‐
70 viously initialized with a different set of attributes. See
71 pthread_mutexattr_setrobust(3C).
72
73
74 ENOMEM Insufficient memory exists to initialize the mutex.
75
76
77 EPERM The caller does not have the privilege to perform the opera‐
78 tion.
79
80
81
82 The pthread_mutex_init() function may fail if:
83
84 EBUSY An attempt was detected to re-initialize the object refer‐
85 enced by mutex, a mutex previously initialized but not yet
86 destroyed.
87
88
89 EINVAL The value specified by attr or mutex is invalid.
90
91
92
93 The pthread_mutex_destroy() function may fail if:
94
95 EBUSY An attempt was detected to destroy the object referenced by
96 mutex while it is locked or referenced (for example, while
97 being used in a pthread_cond_wait(3C) or pthread_cond_timed‐
98 wait(3C)) by another thread.
99
100
101 EINVAL The value specified by mutex is invalid.
102
103
105 See attributes(5) for descriptions of the following attributes:
106
107
108
109
110 ┌─────────────────────────────┬─────────────────────────────┐
111 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
112 ├─────────────────────────────┼─────────────────────────────┤
113 │Interface Stability │Standard │
114 ├─────────────────────────────┼─────────────────────────────┤
115 │MT-Level │MT-Safe │
116 └─────────────────────────────┴─────────────────────────────┘
117
119 pthread_cond_wait(3C), pthread_mutex_lock(3C), pthread_mutexattr_set‐
120 prioceiling(3C), pthread_mutexattr_setprotocol(3C), pthread_mutex‐
121 attr_setpshared(3C), pthread_mutexattr_setrobust(3C), pthread_mutex‐
122 attr_settype(3C), attributes(5), mutex(5), standards(5)
123
124
125
126SunOS 5.11 11 Nov 2008 pthread_mutex_init(3C)