PTHREAD_RWLOCK_WRLOCK(3)
π 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 bylockis 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 π
pthread_rwlock_init(3)pthread_rwlock_rdlock(3)pthread_rwlock_tryrdlock(3)pthread_rwlock_unlock(3)
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
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- nixpkgs unstable: source
- 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