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

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10

NAME

12       pthread_mutex_lock, pthread_mutex_trylock, pthread_mutex_unlock -  lock
13       and unlock a mutex
14

SYNOPSIS

16       #include <pthread.h>
17
18       int pthread_mutex_lock(pthread_mutex_t *mutex);
19       int pthread_mutex_trylock(pthread_mutex_t *mutex);
20       int pthread_mutex_unlock(pthread_mutex_t *mutex);
21
22

DESCRIPTION

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

RETURN VALUE

81       If  successful,  the  pthread_mutex_lock()  and  pthread_mutex_unlock()
82       functions shall return  zero;  otherwise,  an  error  number  shall  be
83       returned to indicate the error.
84
85       The pthread_mutex_trylock() function shall return zero if a lock on the
86       mutex object referenced by mutex is acquired. Otherwise, an error  num‐
87       ber is returned to indicate the error.
88

ERRORS

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

EXAMPLES

129       None.
130

APPLICATION USAGE

132       None.
133

RATIONALE

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

FUTURE DIRECTIONS

163       None.
164

SEE ALSO

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