PTHREAD_RWLOCK_INIT(3) | Library Functions Manual | PTHREAD_RWLOCK_INIT(3)

# NAME

`pthread_rwlock_init`, `pthread_rwlock_destroy`, `pthread_rwlock_rdlock`, `pthread_rwlock_timedrdlock`, `pthread_rwlock_tryrdlock`, `pthread_rwlock_wrlock`, `pthread_rwlock_timedwrlock`, `pthread_rwlock_trywrlock`, `pthread_rwlock_unlock` — operations on read/write locks

# SYNOPSIS

POSIX Threads Library (libpthread, -lpthread)
`#include <pthread.h>`

int
`pthread_rwlock_init`(pthread_rwlock_t *lock, const pthread_rwlockattr_t *attr);

int
`pthread_rwlock_destroy`(pthread_rwlock_t *lock);

int
`pthread_rwlock_rdlock`(pthread_rwlock_t *lock);

int
`pthread_rwlock_timedrdlock`(pthread_rwlock_t *lock, const struct timespec *abstime);

int
`pthread_rwlock_tryrdlock`(pthread_rwlock_t *lock);

int
`pthread_rwlock_wrlock`(pthread_rwlock_t *lock);

int
`pthread_rwlock_timedwrlock`(pthread_rwlock_t *lock, const struct timespec *abstime);

int
`pthread_rwlock_trywrlock`(pthread_rwlock_t *lock);

int
`pthread_rwlock_unlock`(pthread_rwlock_t *lock);

# DESCRIPTION

The `pthread_rwlock_init`() function initializes a read/write lock, with attributes specified by attr. If attr is NULL, the default read/write lock attributes are used. The results of calling `pthread_rwlock_init`() with an already initialized lock are undefined.

The `pthread_rwlock_destroy`() function destroys a read/write lock previously created with `pthread_rwlock_init`().

The `pthread_rwlock_rdlock`() function acquires a read lock on lock provided that lock is not presently held for writing and no writer threads are presently blocked on the lock. If the read lock cannot be immediately acquired, the calling thread blocks until it can acquire the lock.

The `pthread_rwlock_wrlock`() function blocks until a write lock can be acquired against lock.

The `pthread_rwlock_timedrdlock`() and `pthread_rwlock_timedwrlock`() functions perform the same action, but will not wait beyond abstime to obtain the lock before returning.

The `pthread_rwlock_tryrdlock`() and `pthread_rwlock_trywrlock`() functions perform the same action but do not block if the lock cannot be immediately obtained.

The `pthread_rwlock_unlock`() function releases the read/write lock previously obtained.

A thread may hold multiple concurrent read locks. If so, `pthread_rwlock_unlock`() must be called once for each lock obtained.

# RETURN VALUES

If successful, these functions return zero. Otherwise an error number will be returned to indicate the error.

# ERRORS

`pthread_rwlock_init`() fails if:

[`EAGAIN`]
  The system lacked the necessary resources (other than memory) to initialize the lock.
[`ENOMEM`]
  Insufficient memory exists to initialize the lock.
[`EPERM`]
  The caller does not have sufficient privilege to perform the operation.
[`EBUSY`]
  The system has detected an attempt to re-initialize the object referenced by lock, a previously initialized but not yet destroyed read/write lock.
[`EINVAL`]
  The value specified by attr is invalid.

Other functions fail if:

[`EINVAL`]
  The value specified by lock is invalid.
[`ENOMEM`]
  Insufficient memory exists to initialize the lock (applies to statically initialized locks only).

`pthread_rwlock_destroy`() fails if:

[`EBUSY`]
  The system has detected an attempt to destroy the object referenced by lock while it is locked.

`pthread_rwlock_tryrdlock`() and `pthread_rwlock_trywrlock`() fail if:

[`EBUSY`]
  The lock could not be acquired without blocking.

`pthread_rwlock_timedrdlock`() and `pthread_rwlock_timedwrlock`() fail if:

[`ETIMEDOUT`]
  The time specified by abstime was reached before the lock could be obtained.

The `pthread_rwlock_rdlock`(), `pthread_rwlock_timedrdlock`(), and `pthread_rwlock_tryrdlock`() functions fail if:

[`EAGAIN`]
  The lock could not be acquired because the maximum number of read locks against lock has been exceeded.
[`EDEADLK`]
  The current thread already owns lock for writing.

The `pthread_rwlock_wrlock`(), `pthread_rwlock_timedwrlock`(), and `pthread_rwlock_trywrlock`() functions fail if:

[`EDEADLK`]
  The calling thread already owns the read/write lock (for reading or writing).

The `pthread_rwlock_unlock`() fails if:

[`EPERM`]
  The current thread does not own the read/write lock.

# SEE ALSO

pthread_rwlockattr_init(3), pthreads(3)

# STANDARDS

These functions are expected to conform to Version 2 of the Single UNIX Specification (“SUSv2”).

# HISTORY

`pthread_rwlock_init`(), `pthread_rwlock_destroy`(), `pthread_rwlock_rdlock`(), `pthread_rwlock_tryrdlock`(), `pthread_rwlock_wrlock`(), `pthread_rwlock_trywrlock`(), and `pthread_rwlock_unlock`() have been available since OpenBSD 2.5, and `pthread_rwlock_timedrdlock`() and `pthread_rwlock_timedwrlock`() since OpenBSD 4.8.

# BUGS

The PTHREAD_PROCESS_SHARED attribute is not supported.

July 8, 2025 | Debian