PTHREAD_RWLOCK_RDLOCK(3)
π 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 againstlockhas been exceeded. too many besties in the reading room. π«EDEADLKβ the current thread already ownslockfor writing. you're the problem, it's you. π¬EINVALβ the value specified bylockis invalid. not it.ENOMEMβ insufficient memory exists to initialize the lock (applies to statically initialized locks only). the memory said "no πΈ".
π§ SEE ALSO π§
pthread_rwlock_init(3)pthread_rwlock_trywrlock(3)pthread_rwlock_unlock(3)pthread_rwlock_wrlock(3)
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
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- Copyright (c) 1998 Alex Nash
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms