SDL_LockRWLockForReading(3)

πŸ’– SDL_LockRWLockForReading(3) πŸ’–

okay bestie, welcome to the SDL3 FUNCTIONS corner of the manual, this one's literally called SDL_LockRWLockForReading and it's about to be your new fave ✨


🌸 NAME 🌸

SDL_LockRWLockForReading β€” lock the read/write lock for read only operations, no cap 🫢


πŸŽ€ SYNOPSIS πŸŽ€

#include <SDL3/SDL_mutex.h>

void SDL_LockRWLockForReading(SDL_RWLock *rwlock);

✨ DESCRIPTION ✨

okay so. this function will block until the rwlock is available, which is a fancy way of saying it's not locked for writing by any other thread. that's the whole vibe πŸ’…

and here's the cute part: of all the threads waiting to lock the rwlock, all of them may do so at the same time as long as they're requesting read-only access. like, the more the merrier, group chat energy 🌈

but if a thread wants to lock for writing? nope. only one may do so at a time, and no other threads β€” read-only or not β€” may hold the lock at the same time. exclusive. main character energy, truly πŸ‘‘


not me about to tell you the recursive lore πŸ™ˆ it is legal for the owning thread to lock an already-locked rwlock for reading. slay! but it must unlock it the same number of times before it's actually made available for other threads in the system. this is what the girlies call a "recursive rwlock" ✨

πŸ’€ heads up: locking for writing is not recursive. that little perk is only available to read-only locks. periodt.

okay this next bit is the important one so pay attention bestie 😭

it is illegal to request a read-only lock from a thread that already holds the write lock. doing so results in undefined behavior. like, genuinely not the move. unlock the write lock before requesting a read-only lock 🫢

(but, of course, if you already have the write lock, you don't need further locks to read in any case. iykyk πŸ’…)


and the best news ever: this function does not fail πŸŽ‰

  • if rwlock is NULL, it will return immediately having locked nothing. no drama, no error, just vibes.
  • if the rwlock is valid, this function will always block until it can lock the mutex, and return with it locked. iconic behavior honestly πŸ‘‘

πŸ¦‹ FUNCTION PARAMETERS πŸ¦‹

  • rwlock β€” the read/write lock to lock. that's it, that's the whole parameter πŸŽ€

πŸ’œ THREAD SAFETY πŸ’œ

it is safe to call this function from any thread. yes bestie, ANY. go off πŸ’…


⭐ AVAILABILITY ⭐

this function is available since SDL 3.2.0. so if you're on anything older... girl, upgrade 😭


🌈 SEE ALSO 🌈


Simple Directmedia Layer came through with this one, and this page is serving SDL 3.4.18 energy 🫢 go lock those read locks bestie, you got this, mwah πŸ’–βœ¨πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from