Document revision date: 19 July 1999 | |
Previous | Contents | Index |
Obtains the object name from a mutex object.
pthread_mutex_getname_np(
mutex ,
name ,
len );
Argument Data Type Access mutex opaque pthread_mutex_t read name char write len opaque size_t read
mutex
Address of the mutex object whose object name is to be obtained.name
Location to store the obtained object name.len
Length in bytes of buffer at the location specified by name.
This routine copies the object name from the mutex object specified by the mutex argument to the buffer at the location specified by the name argument. Before calling this routine, your program must allocate the buffer indicated by name.The object name is a C language string and provides an identifier that is meaningful to a person debugging a multithreaded application based on DECthreads. The maximum number of characters in the object name is 31.
If the specified condition variable object has not been previously set with an object name, this routine copies a C language null string into the buffer at location name.
Return | Description |
---|---|
0 | Successful completion. |
[EINVAL] | The value specified by mutex is invalid. |
Initializes a mutex with attributes specified by the attr argument.
pthread_mutex_init(
mutex ,
attr );
Argument Data Type Access mutex opaque pthread_mutex_t write attr opaque pthread_mutexattr_t read
mutex
Mutex created.attr
Mutex attributes object to be used to initialize the characteristics of the created mutex.
This routine initializes a mutex with the attributes specified by the mutex attributes object specified in the attr argument. A mutex is a synchronization object that allows multiple threads to serialize their access to shared data.The mutex is initialized and set to the unlocked state. If attr is set to NULL, the default mutex attributes are used. The pthread_mutexattr_settype() routine can be used to specify the type of mutex that is created (normal, recursive, or errorcheck).
See Chapter 2 for more information about mutex usage.
A mutex is a resource of the process, not part of any particular thread. A mutex is neither destroyed nor unlocked automatically when any thread exits. Because mutexes are shared, they may be allocated in heap or static memory, but not on a stack.
Use the PTHREAD_MUTEX_INITIALIZER macro to statically initialize a mutex without calling this routine. Statically initialized mutexes need not be destroyed using pthread_mutex_destroy(). Use this macro as follows:
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZEROnly normal mutexes can be statically initialized.
Return | Description |
---|---|
0 | Successful completion. |
[EAGAIN] | The system lacks the necessary resources to initialize the mutex. |
[ENOMEM] | Insufficient memory exists to initialize the mutex. |
[EBUSY] | The implementation has detected an attempt to reinitialize the mutex (a previously initialized, but not yet destroyed mutex). |
[EINVAL] | The value specified by mutex is invalid. |
[EPERM] | The caller does not have privileges to perform this operation. |
Locks an unlocked mutex. If the mutex is already locked, the calling thread blocks until the mutex becomes available.
pthread_mutex_lock(
mutex );
Argument Data Type Access mutex opaque pthread_mutex_t read
mutex
Mutex to be locked.
This routine locks a mutex with behavior that depends upon the type of mutex, as follows:
- If a normal or default mutex is specified, a deadlock can result if the current owner of the mutex calls this routine in an attempt to lock the mutex a second time. (The deadlock is not detected or reported.)
- If a recursive mutex is specified, the current owner of the mutex can relock the same mutex without blocking. The lock count is incremented for each recursive lock within the thread.
- If an errorcheck mutex is specified and the current owner tries to lock the mutex a second time, this routine reports the [EDEADLK] error. If the mutex is locked by another thread, the calling thread waits for the mutex to become available.
Use the pthread_mutexattr_settype() routine to set the type of the mutex to normal, default, recursive, or errorcheck. For more information about mutexes, see Chapter 2.
The thread that has locked a mutex becomes its current owner and remains the owner until the same thread has unlocked it. This routine returns with the mutex in the locked state and with the calling thread as the mutex's current owner.
A recursive or errorcheck mutex records the identity of the thread that locks it, allowing debuggers to display this information. In most cases, normal and default mutexes do not record the owning thread's identity.
Return | Description |
---|---|
0 | Successful completion. |
[EINVAL] |
The value specified by
mutex is invalid, or
The mutex was created with the protocol attribute set to PTHREAD_PRIO_PROTECT and the calling thread's priority set higher than the mutex's current priority ceiling. |
[EDEADLK] | A deadlock condition is detected. |
Changes the object name in a mutex object.
pthread_mutex_setname_np(
mutex ,
name ,
mbz );
Argument Data Type Access mutex opaque pthread_mutex_t write name char read mbz void read
mutex
Address of the mutex object whose object name is to be changed.name
Object name value to copy into the mutex object.mbz
(Must be zero) Argument for use by DECthreads.
This routine changes the object name in the mutex object specified by the mutex argument to the value specified by the name argument. To set a new mutex object's object name, call this routine immediately after initializing the mutex object.The object name is a C language string and provides an identifier that is meaningful to a person debugging a multithreaded application based on DECthreads. The maximum number of characters in the object name is 31.
Return | Description |
---|---|
0 | Successful completion. |
[EINVAL] | The value specified by mutex is invalid, or the length in characters of name exceeds 31. |
[ENOMEM] | Insufficient memory exists to create a copy of the object name string. |
Attempts to lock the specified mutex. If the mutex is already locked, the calling thread does not wait for the mutex to become available.
pthread_mutex_trylock(
mutex );
Argument Data Type Access mutex opaque pthread_mutex_t read
mutex
Mutex to be locked.
This routine attempts to lock the mutex specified in the mutex argument. When a thread calls this routine, an attempt is made to immediately lock the mutex. If the mutex is successfully locked, this routine returns zero (0) and the calling thread becomes the mutex's current owner. If the specified mutex is locked when a thread calls this routine, the calling thread does not wait for the mutex to become available.The behavior of this routine is as follows:
- For a normal, default, or errorcheck mutex: if the mutex is locked by any thread (including the calling thread) when this routine is called, this routine returns [EBUSY] and the calling thread does not wait to acquire the lock.
- For a normal or errorcheck mutex: if the mutex is not owned, this routine returns zero (0) and the mutex becomes locked.
- For a recursive mutex: if the mutex is owned by the current thread, this routine returns zero (0) and the mutex lock count is incremented. (To unlock a recursive mutex, each call to pthread_mutex_trylock() must be matched by a call to pthread_mutex_unlock().)
Use the pthread_mutexattr_settype() routine to set the mutex type attribute (normal, default, recursive, or errorcheck). For information about mutex types and their usage, see Chapter 2.
Return | Description |
---|---|
0 | Successful completion. |
[EBUSY] | The mutex is already locked; therefore, it was not acquired. |
[EINVAL] |
The value specified by
mutex is invalid, or
The mutex was created with the protocol attribute set to PTHREAD_PRIO_PROTECT and the calling thread's priority set higher than the mutex's current priority ceiling. |
Unlocks the specified mutex.
pthread_mutex_unlock(
mutex );
Argument Data Type Access mutex opaque pthread_mutex_t read
mutex
Mutex to be unlocked.
This routine unlocks the mutex specified by the mutex argument.This routine behaves as follows, based on the type of the specified mutex:
- For a normal, default, or errorcheck mutex: if the mutex is owned by the calling thread, it is unlocked with no current owner. Further, for a normal or default mutex: if the mutex is not locked or is locked by another thread, this routine can also return [EPERM], but this is not guaranteed. For an errorcheck mutex: if the mutex is not locked or is locked by another thread, this routine returns [EPERM].
- For a recursive mutex: if the mutex is owned by the calling thread, the lock count is decremented. The mutex remains locked and owned until the lock count reaches zero (0). When the lock count reaches zero, the mutex becomes unlocked with no current owner.
If one or more threads are waiting to lock the specified mutex, and the mutex becomes unlocked, this routine causes one thread to unblock and to try to acquire the mutex. The scheduling policy is used to determine which thread to unblock. For the SCHED_FIFO and SCHED_RR policies, a blocked thread is chosen in priority order, using first-in/first-out within priorities. Note that the mutex might not be acquired by the awakened thread, if any other running thread attempts to lock the mutex first.
On DIGITAL UNIX, if a signal is delivered to a thread waiting for a mutex, upon return from the signal handler, the thread resumes waiting for the mutex as if it was not interrupted.
Return | Description |
---|---|
0 | Successful completion. |
[EINVAL] | The value specified for mutex is invalid. |
[EPERM] | The calling thread does not own the mutex. |
Calls an initialization routine that is executed by a single thread, once.
pthread_once(
once _control,
init _routine );
Argument Data Type Access once_control opaque pthread_once_t modify init_routine procedure read
once_control
Address of a record that defines the one-time initialization code. Each one-time initialization routine must have its own unique pthread_once_t record.init_routine
Address of a procedure that performs the initialization. This routine is called only once, regardless of the number of times it and its associated once_control are passed to pthread_once().
The first call to this routine by any thread in a process with a given once_control will call the specified init_routine with no arguments. Subsequent calls to pthread_once() with the same once_control will not call the init_routine. On return from pthread_once(), it is guaranteed that the initialization routine has completed.For example, a mutex or a per-thread context key must be created exactly once. Calling pthread_once() ensures that the initialization is serialized across multiple threads. Other threads that reach the same point in the code would be delayed until the first thread is finished.
Note
If you specify an init_routine that directly or indirectly results in a recursive call to pthread_once() and that specifies the same init_routine argument, the recursive call can result in a deadlock.To initialize the once_control record, your program can zero out the entire structure, or you can use the PTHREAD_ONCE_INIT macro, which is defined in the pthread.h header file, to statically initialize that structure. If using PTHREAD_ONCE_INIT, declare the once_control record as follows:
pthread_once_t once_control = PTHREAD_ONCE_INIT;Note that it is often easier to simply lock a statically initialized mutex, check a control flag, and perform necessary initialization (in-line) rather than using pthread_once(). For example, you can code an initialization routine that begins with the following basic logic:
init() { static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; static int flag = FALSE; pthread_mutex_lock(&mutex); if(!flag) { /* initialization code goes here */ flag = TRUE; } pthread_mutex_unlock(&mutex); }
Return | Description |
---|---|
0 | Successful completion. |
[EINVAL] | Invalid argument. |
Destroys a previously initialized read-write lock attributes object.
pthread_rwlockattr_destroy(
attr );
Argument Data Type Access attr opaque pthread_rwlockattr_t write
attr
Address of the read-write lock attributes object to be destroyed.
This routine destroys the read-write lock attributes object referenced by attr; that is, the object becomes uninitialized.After successful completion of this routine, the results of using attr in a call to any routine (other than pthread_rwlockattr_init()) are unpredictable.
Return | Description |
---|---|
0 | Successful completion. |
[EINVAL] | The value specified by attr is invalid. |
Previous | Next | Contents | Index |
privacy and legal statement | ||
6101PRO_022.HTML |