PTHREAD_RWLOCK_WRLOCK(3)

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

πŸ’… PTHREAD_RWLOCK_WRLOCK(3)

okay so this page lives in the Library Functions Manual, and it's all about grabbing a read/write lock for writing. bestie, if you need to be the main character who gets to write while everyone else reads, this is your moment. πŸ’–

✨ NAME ✨

pthread_rwlock_wrlock, pthread_rwlock_trywrlock β€” acquire a read/write lock for writing. literally the whole point. πŸŽ€

🫢 LIBRARY 🫢

POSIX Threads Library (libpthread, -lpthread). yes, link it. πŸ’…

πŸŽ€ SYNOPSIS πŸŽ€

#include <pthread.h>

int
pthread_rwlock_wrlock(pthread_rwlock_t *lock);

int
pthread_rwlock_trywrlock(pthread_rwlock_t *lock);

πŸ’– DESCRIPTION πŸ’–

pthread_rwlock_wrlock() blocks until a write lock can be acquired against lock. it is not rushing; it waits for its turn. 🧚

pthread_rwlock_trywrlock() performs the same action, but it does not block if the lock cannot be immediately obtained. so it's the β€œtry it and see” version, not the β€œstand here forever” version. 😭

🚨 real talk though: the results are undefined if the calling thread already holds the lock at the time the call is made. undefined is not a vibe. do not do that. πŸ’€

πŸ‘‘ IMPLEMENTATION NOTES πŸ‘‘

to prevent writer starvation, writers are favored over readers. periodt. writers get priority so they don't just sit there forever while readers keep cutting the line. 🫢

πŸ”₯ RETURN VALUES πŸ”₯

if successful, pthread_rwlock_wrlock() and pthread_rwlock_trywrlock() will return zero. zero is the slay result. ✨

otherwise an error number will be returned to indicate the error. so check your return value, bestie β€” that's the vibe check. πŸ”

🚨 ERRORS 🚨

the pthread_rwlock_trywrlock() function will fail if:

  • [EBUSY] β€” the calling thread is not able to acquire the lock without blocking. it's giving β€œnot right now, try again later.” πŸ™ˆ

the pthread_rwlock_wrlock() and pthread_rwlock_trywrlock() functions may fail if:

  • [EDEADLK] β€” the calling thread already owns the read/write lock (for reading or writing). bestie, you already have it. no cap. 😭
  • [EINVAL] β€” the value specified by lock is invalid. invalid lock = not iconic. 🚫
  • [ENOMEM] β€” insufficient memory exists to initialize the lock (applies to statically initialized locks only). memory said β€œno <3”. πŸ’€

🌈 SEE ALSO 🌈

go read these too, iykyk. πŸ“šπŸ’–

⭐ STANDARDS ⭐

the pthread_rwlock_wrlock() and pthread_rwlock_trywrlock() functions are expected to conform to Version 2 of the Single UNIX Specification (β€œSUSv2”). fancy. πŸ’…

πŸ¦‹ HISTORY πŸ¦‹

the pthread_rwlock_wrlock() function first appeared in FreeBSD 3.0. a classic. πŸ‘‘


xoxo bestie πŸ’– β€” Debian, August 4, 1998 πŸŽ€ β€” PTHREAD_RWLOCK_WRLOCK(3) out πŸ’…

πŸ“¦ where this page comes from