1PTHREAD_MUTEX_LOCK(P)      POSIX Programmer's Manual     PTHREAD_MUTEX_LOCK(P)
2
3
4

NAME

6       pthread_mutex_lock,  pthread_mutex_trylock, pthread_mutex_unlock - lock
7       and unlock a mutex
8

SYNOPSIS

10       #include <pthread.h>
11
12       int pthread_mutex_lock(pthread_mutex_t *mutex);
13       int pthread_mutex_trylock(pthread_mutex_t *mutex);
14       int pthread_mutex_unlock(pthread_mutex_t *mutex);
15
16

DESCRIPTION

18       The mutex object  referenced  by  mutex  shall  be  locked  by  calling
19       pthread_mutex_lock().  If  the  mutex  is  already  locked, the calling
20       thread shall block until the mutex becomes available.   This  operation
21       shall  return  with  the mutex object referenced by mutex in the locked
22       state with the calling thread as its owner.
23
24       If the mutex type is PTHREAD_MUTEX_NORMAL, deadlock detection shall not
25       be  provided.  Attempting  to  relock  the  mutex causes deadlock. If a
26       thread attempts to unlock a mutex that it has not  locked  or  a  mutex
27       which is unlocked, undefined behavior results.
28
29       If  the  mutex  type  is  PTHREAD_MUTEX_ERRORCHECK, then error checking
30       shall be provided. If a thread attempts to relock a mutex that  it  has
31       already  locked,  an  error  shall be returned. If a thread attempts to
32       unlock a mutex that it has not locked or a mutex which is unlocked,  an
33       error shall be returned.
34
35       If  the  mutex  type  is  PTHREAD_MUTEX_RECURSIVE, then the mutex shall
36       maintain the concept of  a  lock  count.  When  a  thread  successfully
37       acquires  a  mutex  for  the first time, the lock count shall be set to
38       one. Every time a thread relocks this mutex, the lock  count  shall  be
39       incremented  by  one.  Each time the thread unlocks the mutex, the lock
40       count shall be decremented by one. When the lock  count  reaches  zero,
41       the  mutex  shall  become  available for other threads to acquire. If a
42       thread attempts to unlock a mutex that it has not  locked  or  a  mutex
43       which is unlocked, an error shall be returned.
44
45       If  the  mutex type is PTHREAD_MUTEX_DEFAULT, attempting to recursively
46       lock the mutex results in undefined behavior. Attempting to unlock  the
47       mutex  if  it was not locked by the calling thread results in undefined
48       behavior. Attempting to unlock the mutex if it is not locked results in
49       undefined behavior.
50
51       The   pthread_mutex_trylock()   function   shall   be   equivalent   to
52       pthread_mutex_lock(), except that if the  mutex  object  referenced  by
53       mutex  is  currently  locked  (by  any  thread,  including  the current
54       thread), the call shall  return  immediately.  If  the  mutex  type  is
55       PTHREAD_MUTEX_RECURSIVE and the mutex is currently owned by the calling
56       thread, the mutex lock count  shall  be  incremented  by  one  and  the
57       pthread_mutex_trylock() function shall immediately return success.
58
59       The pthread_mutex_unlock() function shall release the mutex object ref‐
60       erenced by mutex.    The manner in which a mutex is released is  depen‐
61       dent  upon the mutex's type attribute.  If there are threads blocked on
62       the mutex object referenced by  mutex  when  pthread_mutex_unlock()  is
63       called,  resulting in the mutex becoming available, the scheduling pol‐
64       icy shall determine which thread shall acquire the mutex.
65
66       (In the case of PTHREAD_MUTEX_RECURSIVE mutexes, the mutex shall become
67       available  when the count reaches zero and the calling thread no longer
68       has any locks on this mutex.)
69
70       If a signal is delivered to a thread waiting for a mutex,  upon  return
71       from  the  signal handler the thread shall resume waiting for the mutex
72       as if it was not interrupted.
73

RETURN VALUE

75       If  successful,  the  pthread_mutex_lock()  and  pthread_mutex_unlock()
76       functions  shall  return  zero;  otherwise,  an  error  number shall be
77       returned to indicate the error.
78
79       The pthread_mutex_trylock() function shall return zero if a lock on the
80       mutex  object referenced by mutex is acquired. Otherwise, an error num‐
81       ber is returned to indicate the error.
82

ERRORS

84       The pthread_mutex_lock() and  pthread_mutex_trylock()  functions  shall
85       fail if:
86
87       EINVAL The  mutex  was  created  with the protocol attribute having the
88              value PTHREAD_PRIO_PROTECT and the calling thread's priority  is
89              higher than the mutex's current priority ceiling.
90
91
92       The pthread_mutex_trylock() function shall fail if:
93
94       EBUSY  The mutex could not be acquired because it was already locked.
95
96
97       The       pthread_mutex_lock(),       pthread_mutex_trylock(),      and
98       pthread_mutex_unlock() functions may fail if:
99
100       EINVAL The value specified by mutex does not refer  to  an  initialized
101              mutex object.
102
103       EAGAIN The  mutex  could  not be acquired because the maximum number of
104              recursive locks for mutex has been exceeded.
105
106
107       The pthread_mutex_lock() function may fail if:
108
109       EDEADLK
110              The current thread already owns the mutex.
111
112
113       The pthread_mutex_unlock() function may fail if:
114
115       EPERM  The current thread does not own the mutex.
116
117
118       These functions shall not return an error code of [EINTR].
119
120       The following sections are informative.
121

EXAMPLES

123       None.
124

APPLICATION USAGE

126       None.
127

RATIONALE

129       Mutex objects are intended to serve as a low-level primitive from which
130       other  thread  synchronization  functions  can  be  built. As such, the
131       implementation of mutexes should be as efficient as possible, and  this
132       has ramifications on the features available at the interface.
133
134       The  mutex  functions  and the particular default settings of the mutex
135       attributes have been motivated by the  desire  to  not  preclude  fast,
136       inlined implementations of mutex locking and unlocking.
137
138       For  example, deadlocking on a double-lock is explicitly allowed behav‐
139       ior in order to avoid requiring more overhead in  the  basic  mechanism
140       than  is  absolutely  necessary.  (More  "friendly" mutexes that detect
141       deadlock or that allow multiple locking by the same thread  are  easily
142       constructed by the user via the other mechanisms provided. For example,
143       pthread_self() can be used to record mutex ownership.)  Implementations
144       might also choose to provide such extended features as options via spe‐
145       cial mutex attributes.
146
147       Since most attributes only need to be checked when a thread is going to
148       be  blocked,  the  use  of attributes does not slow the (common) mutex-
149       locking case.
150
151       Likewise, while being able to extract the thread ID of the owner  of  a
152       mutex  might  be desirable, it would require storing the current thread
153       ID when each mutex is locked, and this could incur unacceptable  levels
154       of overhead. Similar arguments apply to a mutex_tryunlock operation.
155

FUTURE DIRECTIONS

157       None.
158

SEE ALSO

160       pthread_mutex_destroy()  , pthread_mutex_timedlock() , the Base Defini‐
161       tions volume of IEEE Std 1003.1-2001, <pthread.h>
162
164       Portions of this text are reprinted and reproduced in  electronic  form
165       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
166       -- Portable Operating System Interface (POSIX),  The  Open  Group  Base
167       Specifications  Issue  6,  Copyright  (C) 2001-2003 by the Institute of
168       Electrical and Electronics Engineers, Inc and The Open  Group.  In  the
169       event of any discrepancy between this version and the original IEEE and
170       The Open Group Standard, the original IEEE and The Open Group  Standard
171       is  the  referee document. The original Standard can be obtained online
172       at http://www.opengroup.org/unix/online.html .
173
174
175
176IEEE/The Open Group                  2003                PTHREAD_MUTEX_LOCK(P)
Impressum