pthread_rwlock_tryrdlock(3)

pthread_rwlock_tryrdlock3

Acquire a read/write lock for reading

Part of pthread_rwlock_rdlock(3).

Usage

#include <pthread.h>

int

pthread_rwlock_rdlock(pthread_rwlock_t *lock);

int

pthread_rwlock_tryrdlock(pthread_rwlock_t *lock);

Library

POSIX Threads Library (libpthread, -lpthread)

Overview

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_tryrdlock() function performs the same action, but does not block if the lock cannot be immediately obtained (i.e., the lock is held for writing or there are waiting writers).

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

The results of acquiring a read lock while the calling thread holds a write lock are undefined.

Implementation notes

To prevent writer starvation, writers are favored over readers.

Return values

If successful, the pthread_rwlock_rdlock() and pthread_rwlock_tryrdlock() functions will return zero. Otherwise an error number will be returned to indicate the error.

Errors

The pthread_rwlock_tryrdlock() function will fail if:

[EBUSY]

The lock could not be acquired because a writer holds the lock or was blocked on it.

The pthread_rwlock_rdlock() and pthread_rwlock_tryrdlock() functions may 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.

[EINVAL]

The value specified by lock is invalid.

[ENOMEM]

Insufficient memory exists to initialize the lock (applies to statically initialized locks only).

Releases

70 releases since FreeBSD 4.0-RELEASE; the text changed 37 times.

  1. 4.0
  2. 4.1–4.4
  3. 4.5–4.11
  4. 5.0–5.2.1
  5. 5.3–5.5
  6. 6.0–6.3
  7. 6.4
  8. 7.0
  9. 7.1
  10. 7.2
  11. 7.3
  12. 7.4
  13. 8.0
  14. 8.1
  15. 8.2
  16. 8.3
  17. 8.4
  18. 9.0
  19. 9.1
  20. 9.2
  21. 9.3
  22. 10.0
  23. 10.1
  24. 10.2
  25. 10.3
  26. 10.4
  27. 11.0
  28. 11.1
  29. 11.2
  30. 11.3
  31. 11.4
  32. 12.0
  33. 12.1
  34. 12.2
  35. 12.3
  36. 12.4
  37. 13.0–13.2
  38. 13.3–16.0

Standards

The pthread_rwlock_rdlock() and pthread_rwlock_tryrdlock() functions are expected to conform to Version 2 of the Single UNIX Specification (“SUSv2”).

History

The pthread_rwlock_rdlock() function first appeared in FreeBSD 3.0.

From base 16.0-CURRENT-20261027-61c70dc16a79 in FreeBSD 16.0-CURRENT (2026-10-27). Parsed by carta from the page's mdoc source.