SDL_CreateRWLock(3)
β¨ SDL_CreateRWLock(3) β¨
okay bestie, welcome to the read/write lock factory π this page lives in the SDL3 functions section of the manual, and it's literally just about making one (1) brand new rwlock. let's get into it π«Ά
πΈ NAME πΈ
SDL_CreateRWLock β Create a new read/write lock.
π SYNOPSIS π
#include <SDL3/SDL_mutex.h>
SDL_RWLock * SDL_CreateRWLock(void);
π¦ DESCRIPTION π¦
so picture this: you've got a resource that everybody wants to read and basically nobody wants to change. that's the rwlock's whole moment. a read/write lock is useful for situations where you have multiple threads trying to access a resource that is rarely updated. all the threads asking for a read-only lock get to run in parallel, no fighting, no queue β iconic. but the second a thread asks for a write lock, it gets exclusive access, like the bouncer at the door. which means it's safe for multiple threads to use a resource at the same time if they promise not to change it, and when it does have to be changed, the rwlock acts as a gateway making sure those changes can be made safely. no chaos, no drama π
in the right situation, a rwlock can be more efficient than a mutex β because a mutex only lets a single thread proceed at a time, even if that thread isn't even modifying the data. rude, honestly. π
every newly-created read/write lock begins in the unlocked state. fresh out the box, chill, no one's holding it β¨
heads up though: calls to SDL_LockRWLockForReading() and SDL_LockRWLockForWriting will not return while the rwlock is locked for writing by another thread. they'll just... wait there. if you don't vibe with blocking, go look at SDL_TryLockRWLockForReading() and SDL_TryLockRWLockForWriting() to attempt to lock without blocking π
now the important lore, because this is where people get got:
- SDL read/write locks are only recursive for read-only locks! π
- They are not guaranteed to be fair, or provide access in a FIFO manner!
- They are not guaranteed to favor writers.
- You may not lock a rwlock for both read-only and write access at the same time from the same thread β so you can't promote your read-only lock to a write lock without unlocking first. no glow-ups mid-lock, sorry bestie π
fr fr read those last four bullets again. they are not decorative. they will find you. π
π RETURN VALUE π
returns the initialized and unlocked read/write lock β or NULL on failure. if you get NULL, call SDL_GetError() for more information, she'll tell you what went wrong π₯Ί
π THREAD SAFETY π
it is safe to call this function from any thread. go off, all of you, simultaneously π β¨
β AVAILABILITY β
this function is available since SDL 3.2.0.
π₯ SEE ALSO π₯
SDL_DestroyRWLock(3)SDL_LockRWLockForReading(3)SDL_LockRWLockForWriting(3)SDL_TryLockRWLockForReading(3)SDL_TryLockRWLockForWriting(3)SDL_UnlockRWLock(3)
okay that's the whole page, you ate it, go lock something responsibly π«Άπ this lil writeup came from Simple Directmedia Layer, covering SDL 3.4.18, and it's the SDL_CreateRWLock(3) page β mwah, periodt. π
π¦ where this page comes from
- fedora 43: SDL3-devel 3.4.18-1.fc43 Β· Zlib AND MIT AND Apache-2.0 AND (Apache-2.0 OR MIT)
- fedora 43: SDL3-devel 3.4.18-1.fc43 Β· Zlib AND MIT AND Apache-2.0 AND (Apache-2.0 OR MIT)
- fedora 44: SDL3-devel 3.4.18-1.fc44 Β· Zlib AND MIT AND Apache-2.0 AND (Apache-2.0 OR MIT)
- fedora 44: SDL3-devel 3.4.18-1.fc44 Β· Zlib AND MIT AND Apache-2.0 AND (Apache-2.0 OR MIT)
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms