PTHREAD...YWRLOCK(3POSIX)
π PTHREAD_RWLOCK_TRYWRLOCK, PTHREAD_RWLOCK_WRLOCK π
okay bestie, welcome to the POSIX Programmer's Manual β this little page is part of it, and honestly it's giving "gatekeeping the write lock" energy.
β οΈ real talk before we start: the Linux implementation of this interface may differ (so like, go check the corresponding Linux manual page for the deets on Linux behavior), or the interface might not even be implemented on Linux at all. scream. π
β¨ NAME β¨
pthread_rwlock_trywrlock, pthread_rwlock_wrlock β lock a read-write lock object for writing π₯
β¨ SYNOPSIS β¨
#include <pthread.h>
int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
β¨ DESCRIPTION β¨
So, pthread_rwlock_trywrlock() applies a write lock exactly like pthread_rwlock_wrlock() does β with one exception: it shall fail if any thread currently holds rwlock (for reading or writing). like, no waiting, no queuing, just a polite no. π
Meanwhile pthread_rwlock_wrlock() shall apply a write lock to the read-write lock referenced by rwlock. The calling thread shall acquire that write lock if no thread (reader or writer) holds the read-write lock rwlock. Otherwise β if another thread holds it β the calling thread shall block until it can acquire the lock. and if a deadlock condition occurs, or the calling thread already owns the read-write lock for writing or reading? the call shall either deadlock or return [EDEADLK]. pick your poison, bestie. π
π¨ Results are undefined if any of these functions are called with an uninitialized read-write lock. uninit'd lock = chaos, we don't do that here.
And one more lil detail that matters: if a signal is delivered to a thread waiting for a read-write lock for writing, upon return from the signal handler the thread resumes waiting for the read-write lock for writing as if it was not interrupted. iconic behavior honestly, it just picks right back up. β¨
β¨ RETURN VALUE β¨
pthread_rwlock_trywrlock() shall return zero if the lock for writing on the read-write lock object referenced by rwlock is acquired. Otherwise, an error number shall be returned to indicate the error. π
If successful, pthread_rwlock_wrlock() shall return zero; otherwise, an error number shall be returned to indicate the error.
β¨ ERRORS β¨
The pthread_rwlock_trywrlock() function shall fail if:
EBUSYβ The read-write lock could not be acquired for writing because it was already locked for reading or writing. (she's taken, babes π )
The pthread_rwlock_wrlock() function may fail if:
EDEADLKβ A deadlock condition was detected or the current thread already owns the read-write lock for writing or reading. π circular dependency moment π
π«Ά These functions shall not return an error code of [EINTR]. so don't come looking for it.
The following sections are informative. (translation: the tea below is bonus content, not rules π)
β¨ EXAMPLES β¨
None. π literally none. the examples section said "not today." go write your own, main character.
β¨ APPLICATION USAGE β¨
Applications using these functions may be subject to priority inversion, as discussed in the Base Definitions volume of POSIX.1β2017, Section 3.291, Priority Inversion. iykyk
β¨ RATIONALE β¨
If an implementation detects that the value specified by the rwlock argument to pthread_rwlock_trywrlock() or pthread_rwlock_wrlock() does not refer to an initialized read-write lock object, it is recommended that the function should fail and report an [EINVAL] error. that's the recommendation, bestie β noted, logged, respected.
β¨ FUTURE DIRECTIONS β¨
None. β¨ nothing on the horizon, we're thriving in the present.
β¨ SEE ALSO β¨
pthread_rwlock_destroy()pthread_rwlock_rdlock()pthread_rwlock_timedrdlock()pthread_rwlock_timedwrlock()pthread_rwlock_unlock()
The Base Definitions volume of POSIX.1β2017, Section 3.291, Priority Inversion, Section 4.12, Memory Synchronization, <pthread.h>
β¨ COPYRIGHT β¨
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
k that's the whole page bestie π«Ά shoutout to IEEE/The Open Group, 2017 for the source and the date β they ate and left no crumbs. PTHREAD...YWRLOCK(3POSIX) out. π
β¨πΈ
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms