Книга: Programming with POSIX® Threads

9.3.4 Mutexes

9.3.4 Mutexes

Mutexes provide synchronization, the ability to control how threads share resources. You use mutexes to prevent multiple threads from modifying shared data at the same time, and to ensure that a thread can read consistent values for a set of resources (for example, memory) that may be modified by other threads.

pthread_mutexattr_destroy

int pthread_mutexattr_destroy (

pthread_mutexattr_t *attr);

Destroy a mutex attributes object. The object can no longer be used.

References: 3.2, 5.2.1

Headers: <pthread.h>

Errors: [EINVAL]attrinvalid.

Hint: Does not affect mutexes created using attr.

pthread_mutexattr_getpshared..............................................[_POSIX_THREAD_PROCESS_SHARED]

int pthread_mutexattr_getpshared (

const pthread_mutexattr_t *attr,

int *pshared);

Determine whether mutexes created with attr can be shared by multiple processes.

pshared

PTHREAD_PROCESS_SHARED

PTHREAD_PROCESS_PRIVATE

May be shared if in shared memory.

Cannot be shared.

References: 3.2,5.2.1

Headers: <pthread.h>

Errors: [EINVAL]attrinvalid.

Hint: pshared mutexes must be allocated in shared memory.

pthread_mutexattr_init

int pthread_mutexattr_init (

pthread_mutexattr_t *attr);

Initialize a mutex attributes object with default attributes.

References: 3.2,5.2.1

Headers: <pthread.h>

Errors: [ENOMEM] insufficient memory for attr.

Hint: Use to define mutex types.

pthread_mutexattr_setpshared...............................................[_POSIX_THREAD_PROCESS_sHARED ]

int pthread_mutexattr_setpshared (

pthread_mutexattr_t *attr,

int pshared);

Mutexes created with attr can be shared between processes if the pthread_mutex_t variable is allocated in memory shared by the processes.

pshared

PTHREAD_PROCESS_SHARED

May be shared if in shared memory.

PTHREAD_PROCESS_PRIVATE

Cannot be shared.

References: 3.2,5.2.1

Headers: <pthread.h>

Errors: [EINVAL] attr or detachstate invalid.

Hint: pshared mutexes must be allocated in shared memory.

pthread_mutex_destroy

int pthread_mutex_destroy (

pthread_mutex_t *mutex);

Destroy a mutex that you no longer need.

References: 3.2,5.2.1

Headers: <pthread.h>

Errors: [EBUSY] mutex is in use.

[EINVAL] mutex is invalid. Hint: Safest after unlocking mutex, when no other threads will lock.

pthread_mutex_init

int pthread_mutex_init ( pthread_mutex_t const pthread_mutexattr_t

*mutex, *attr);

Initialize a mutex. The attr argument specifies optional creation attributes.

References:

Headers:

Errors:

Hint:

3.2, 5.2.1 <pthread.h>

[EAGAIN] insufficient resources (other than memory).

[ENOMEM] insufficient memory.

[EPERM] no privilege to perform operation.

[EBUSY] mutex is already initialized.

[EINVAL] attr is invalid.

Use static initialization instead, if possible.

pthread_mutex_lock

int pthread_mutex_lock (

pthread_mutex_t *mutex);

Lock a mutex. If the mutex is currently locked, the calling thread is blocked until mutex is unlocked. On return, the thread owns the mutex until it calls pthread_ mutex_unlock.

References: 3.2, 5.2.1 Headers: <pthread.h>

Errors: [EINVAL] thread priority exceeds mutex priority ceiling.

[EINVAL] mutex is invalid.

[EDEADLK] calling thread already owns mutex. Hint: Always unlock within the same thread.

pthread_mutex_trylock

int pthread_mutex_trylock (

pthread_mutex_t *mutex);

Lock a mutex. If the mutex is currently locked, returns immediately with EBUSY. Otherwise, calling thread becomes owner until it unlocks.

References: 3.2,5.2.1 Headers: <pthread.h>

Errors: [EINVAL] thread priority exceeds mutex priority ceiling.

[EBUSY] mutex is already locked.

[EINVAL] mutex is invalid.

[EDEADLK] calling thread already owns mutex. Hint: Always unlock within the same thread.

pthread_mutex_unlock

int pthread_mutex_unlock (

pthread_mutex_t *mutex);

Unlock a mutex. The mutex becomes unowned. If any threads are waiting for the mutex, one is awakened (scheduling policy SCHED_FIFO and SCHED_RR policy waiters are chosen in priority order, then any others are chosen in unspecified order).

References: 3.2, 5.2.1

Headers: <pthread.h>

Errors: [EINVAL]mutex is invalid.

[EPERM] calling thread does not own mutex. Hint: Always unlock within the same thread.

Оглавление книги


Генерация: 0.041. Запросов К БД/Cache: 0 / 2
поделиться
Вверх Вниз