PTHREAD_RWLOCK_RDLOCK(3)

πŸ“¦ version 15.1, in freebsd 14.5, freebsd 15.1

πŸ’… PTHREAD_RWLOCK_RDLOCK πŸ’…

okay bestie, so this one is from the POSIX Threads Library β€” that's libpthread, link it with -lpthread β€” and it lives in section 3 of the manual (Library Functions Manual, iykyk). it's giving "let me read the tea without blocking the group chat" energy. let's go 🫢


✨ NAME ✨

pthread_rwlock_rdlock, pthread_rwlock_tryrdlock β€” acquire a read/write lock for reading πŸ“–


πŸŽ€ LIBRARY πŸŽ€

POSIX Threads Library (libpthread, -lpthread)


πŸ¦‹ SYNOPSIS πŸ¦‹

#include <pthread.h>

int
pthread_rwlock_rdlock(pthread_rwlock_t *lock);

int
pthread_rwlock_tryrdlock(pthread_rwlock_t *lock);

🌸 DESCRIPTION 🌸

so pthread_rwlock_rdlock() grabs a read lock on lock, and the vibe check it has to pass is: the lock is not currently held for writing, and no writer threads are blocked on it right now. if it can't get that read lock immediately, the calling thread just blocks β€” like, sits there, main character waiting room β€” until it can actually acquire it. 😭

pthread_rwlock_tryrdlock() does the exact same thing, but does not block if the lock can't be obtained right away β€” meaning the lock is held for writing, or there are writers waiting. it's a "no thanks, not today" function. πŸ™ˆ

a thread can hold multiple concurrent read locks β€” obsessed, very generous of it β€” and if it does, you gotta call pthread_rwlock_unlock() once for each lock it obtained. no skipping, no shortcuts. πŸ’…

⚠️ real talk: acquiring a read lock while the calling thread already holds a write lock? the results are undefined. don't do it, bestie. that's not a vibe. πŸ’€

πŸ”₯ IMPLEMENTATION NOTES πŸ”₯

to prevent writer starvation, writers are favored over readers. them's the rules. πŸ‘‘


⭐ RETURN VALUES ⭐

if everything's slay, both pthread_rwlock_rdlock() and pthread_rwlock_tryrdlock() return zero. otherwise you get an error number back to tell you what went wrong. no cap.


πŸ’œ ERRORS πŸ’œ

so pthread_rwlock_tryrdlock() will fail if:

  • EBUSY β€” the lock could not be acquired because a writer holds the lock, or a writer was blocked on it. it's occupied, babes. 🚫

and both pthread_rwlock_rdlock() and pthread_rwlock_tryrdlock() may fail if:

  • EAGAIN β€” the lock could not be acquired because the maximum number of read locks against lock has been exceeded. too many besties in the reading room. πŸ«‚
  • EDEADLK β€” the current thread already owns lock for writing. you're the problem, it's you. 😬
  • EINVAL β€” the value specified by lock is invalid. not it.
  • ENOMEM β€” insufficient memory exists to initialize the lock (applies to statically initialized locks only). the memory said "no πŸ’Έ".

🧚 SEE ALSO 🧚

the whole lock friend group, honestly. 🌈


πŸ‘‘ STANDARDS πŸ‘‘

pthread_rwlock_rdlock() and pthread_rwlock_tryrdlock() are expected to conform to Version 2 of the Single UNIX Specification (that's β€œSUSv2” to the girlies). ✨


🌻 HISTORY 🌻

pthread_rwlock_rdlock() first appeared in FreeBSD 3.0. a true icon of the era. πŸ’…


okay that's the whole page, mwah πŸ“. this manual page was brought to you from Debian, dated August 4, 1998 β€” yes, she's been iconic since '98, no notes, periodt. πŸ’–πŸŽ€

πŸ“¦ where this page comes from