Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

pthread_mutexattr_setkind_np(3)

pthread_mutex_destroy(3)

pthread_mutex_init(3)

pthread_mutex_trylock(3)

pthread_mutex_unlock(3)

pthread_mutex_lock(3)  —  Subroutines

Digital

NAME

pthread_mutex_lock − Locks an unlocked mutex. If the mutex is locked, this routine causes the thread to wait for the mutex to become available. 

SYNOPSIS

#include <pthread.h>
int pthread_mutex_lock(

pthread_mutex_t ∗mutex );

PARAMETERS

mutexMutex locked. 

DESCRIPTION

This routine locks a mutex. If the mutex is locked when a thread calls this routine, the thread waits for the mutex to become available. 

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 current thread as the mutex’s current owner. 

If you specified a fast mutex in a call to pthread_mutexattr_setkind_np, a deadlock can result if the current owner of a mutex calls this routine in an attempt to lock the mutex a second time. If you specified a recursive mutex in a call to pthread_mutexattr_setkind_np, the current owner of a mutex can relock the same mutex without blocking. 

The preemption of a lower-priority thread that locks a mutex might result in the indefinite blocking of higher-priority threads waiting for the same mutex. The execution of the waiting, higher-priority threads are blocked for as long as there is a sufficient number of runable threads of any priority between the lower- and higher- priority values. Priority inversion occurs when any resource is shared between threads with different priorities. 

Priority inversion is a problem because it counters the intent of scheduling policy by defeating the effect of priority.  Following are possible solutions that an implementation can choose to prevent or reduce the severity of this problem:

       •Increase the priority of a thread when it first locks a mutex. 

       •Increase the priority of the owner of a mutex whenever a thread of higher priority starts to wait for the same mutex. 

       •Adjust thread priorities to ensure that no ready thread is blocked indefinitely from access to a processor. (Note that this solution applies to all cases of priority inversion, not just those associated with mutexes.) 

RETURN VALUES

If an error condition occurs, this routine returns −1 and sets errno to the corresponding error value. Possible return values are as follows:

Return Error Description
 0 Successful completion.
−1 [EINVAL] The value specified by mutex is invalid.
−1 [EDEADLK] A deadlock condition is detected.

RELATED INFORMATION

pthread_mutexattr_setkind_np(3), pthread_mutex_destroy(3), pthread_mutex_init(3), pthread_mutex_trylock(3), pthread_mutex_unlock(3)

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026