pthread_rwlockattr_getkind_np, pthread_rwlockattr_setkind_np

Name

pthread_rwlockattr_getkind_np, pthread_rwlockattr_setkind_np -- get/set the read-write lock kind of the thread read-write lock attribute object

Synopsis

#include <pthread.h>

int pthread_rwlockattr_getkind_np(const pthread_rwlockattr_t * attr, int * pref);

int pthread_rwlockattr_setkind_np(pthread_rwlockattr_t * attr, int * pref);

Description

The pthread_rwlockattr_setkind_np() function sets the kind of read-write lock of the thread read-write lock attribute object referred to by attr to the value specified with pref. The argument pref may be set to PTHREAD_RWLOCK_PREFER_READER_NP, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP, or PTHREAD_RWLOCK_PREFER_WRITER_NP. The default lock setting is PTHREAD_RWLOCK_PREFER_READER_NP. A thread may hold multiple read locks, i.e. read locks are recursive. According to The Single Unix Specification, the behavior is unspecified when a reader tries to place a lock, and there is no write lock but writers are waiting. Giving preference to the reader, as is set by default with the PTHREAD_RWLOCK_PREFER_READER_NP value implies that the reader will receive the requested lock, even if a writer is waiting. As long as there are readers the writer will be starved. Setting the kind to PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP, avoids writer starvation as long as any read locking is not done in a recursive fashion. The pthread_rwlockattr_getkind_np() function returns the value of the read-write lock attribute of the thread read-write lock attribute object referred to by attr in the pointer pref.

Return Value

pthread_rwlockattr_setkind_np() function returns 0 on success; on error, it returns a non-zero error number. pthread_rwlockattr_setkind_np() function always returns 0.

Errors

EINVAL 

pref is set to an unsupported value.

Notes

Setting the value read-write lock kind to PTHREAD_RWLOCK_PREFER_WRITER_NP, results in the same behavior as setting the value to PTHREAD_RWLOCK_PREFER_READER_NP. As long as a reader thread holds the lock the thread holding a write lock will be starved. Setting the kind value to PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP, allows the writer to run. However, the writer may not be recursive as is implied by the name.