SDL_LockMutex(3)

πŸ’… SDL_LockMutex πŸ’…

okay bestie, welcome to the SDL3 FUNCTIONS section of the manual, and today's main character is SDL_LockMutex(3). grab a snack, we're locking a mutex 🫢


✨ NAME ✨

SDL_LockMutex β€” lock the mutex. that's it. that's the whole job. iconic, honestly πŸ’–


✨ SYNOPSIS ✨

#include <SDL3/SDL_mutex.h>

void SDL_LockMutex(SDL_Mutex *mutex);

✨ DESCRIPTION ✨

so here's the vibe 🌸 this function blocks until the mutex is available. and "available" means it's in the unlocked state and the OS has chosen the caller as the next thread to lock it. of all the threads waiting to lock that mutex, only one may do it at a time. no cutting the line, no group-chat pile-on. periodt πŸ’…

now, plot twist: it is legal for the owning thread to lock an already-locked mutex πŸ‘‘ like, the thread that already holds it can lock it again. but β€” and this is the part where you gotta listen, bestie β€” it must unlock it the same number of times before the mutex is actually made available for other threads in the system. that's what we call a "recursive mutex" πŸŒ€ lock it three times, unlock it three times, or everyone else is stuck waiting forever. no cap.

and here's the chill part: this function does not fail 😌 if mutex is NULL, it will return immediately, having locked nothing. literally nothing happened, we just vibed and left πŸ™ˆ if the mutex is valid, this function will always block until it can lock the mutex, and return with it locked. so it either no-ops on you or it commits. no in-between.

no return value to check, no error code, no drama. it locks or it doesn't. that's the energy πŸ’€

✨ FUNCTION PARAMETERS ✨

  • mutex β€” the mutex to lock. the one and only glow-up target πŸŽ€

✨ THREAD SAFETY ✨

it is safe to call this function from any thread 🌈 go off, every thread, we love a function with no gatekeeping ⭐


✨ AVAILABILITY ✨

this function is available since SDL 3.2.0 πŸ¦‹ so if you're on anything older, bestie, it's giving "not in your version" 😭


✨ SEE ALSO ✨


Simple Directmedia Layer β€” SDL 3.4.18 πŸŽ€ that's the source and version, mwah, thanks for reading bestie, go forth and lock responsibly, SDL_LockMutex(3) out πŸ’…βœ¨πŸ«Ά

πŸ“¦ where this page comes from