SDL_TryLockRWLockForWriting(3)

SDL_TryLockRWLockForWriting(3) πŸ’–

okay bestie, this one lives in the SDL3 FUNCTIONS corner of the manual and it is exactly the low-commitment lock you've been looking for. no waiting, no pining, no blocking. it's giving "if you're free, cool, if not, i'm literally walking away." πŸ’…


✨ NAME ✨

SDL_TryLockRWLockForWriting β€” try to lock a read/write lock for writing without blocking. πŸŽ€


🌸 SYNOPSIS 🌸

#include <SDL3/SDL_mutex.h>

bool SDL_TryLockRWLockForWriting(SDL_RWLock *rwlock);

πŸ’œ DESCRIPTION πŸ’œ

so this works just like SDL_LockRWLockForWriting(), except β€” and this is the whole vibe β€” if the rwlock isn't available, it returns false immediately instead of making you sit there. no cap. πŸ¦‹

this technique is chef's kiss when you need exclusive access to a resource but you do not want to wait around for it, and you're fully planning to come back and try again later. main character energy: you ask, you get told no, you move on with your day. ✨

πŸ’€ now the serious part, bestie, listen up. it is illegal for the owning thread to lock an already-locked rwlock for writing. read-only may be locked recursively β€” writing can not. doing so results in undefined behavior. not "kinda weird behavior," not "a warning," undefined. πŸ™ˆ
😭 also illegal: requesting a write lock from a thread that already holds a read-only lock. again: undefined behavior. you have to unlock the read-only lock before requesting a write lock. unlock first, then ask. periodt. πŸ‘‘

and the cutest little quirk: this function returns true if you pass it a NULL rwlock. yeah. NULL? true. iconic behavior honestly. 🌈


🫢 FUNCTION PARAMETERS 🫢

  • rwlock β€” the rwlock to try to lock. that's the one. that's the whole list. πŸŽ€

⭐ RETURN VALUE ⭐

  • Returns true on success πŸ’–
  • Returns false if the lock would block πŸ˜”

πŸ”₯ THREAD SAFETY πŸ”₯

It is safe to call this function from any thread. any of them. all of them. go wild. 🌸


🌟 AVAILABILITY 🌟

This function is available since SDL 3.2.0. so if you're on something older… bestie. upgrade. 🧚


πŸ‘‘ SEE ALSO πŸ‘‘


and that's the page!! brought to you by Simple Directmedia Layer, running on SDL 3.4.18, filed under SDL_TryLockRWLockForWriting(3). go forth and try-lock responsibly, bestie πŸ«ΆπŸ’–βœ¨

πŸ“¦ where this page comes from