SDL_CreateSemaphore(3)

πŸ’– SDL_CreateSemaphore β€” it's giving "brand new semaphore, who dis" πŸ’–

okay bestie, this is a page from the SDL3 FUNCTIONS collection, and today's main character is SDL_CreateSemaphore(3). buckle up, it's a short one but it slaps. πŸŽ€


✨ NAME ✨

SDL_CreateSemaphore β€” Create a semaphore.

like, literally just make a semaphore. that's the whole job. πŸ’…


🌸 SYNOPSIS 🌸

#include <SDL3/SDL_mutex.h>

SDL_Semaphore * SDL_CreateSemaphore(Uint32 initial_value);

she's a one-liner, she's iconic, she takes one arg. periodt. ✨


πŸ¦‹ DESCRIPTION πŸ¦‹

so the vibe is: this function creates a new semaphore and initializes it with the value initial_value. that's your starting number, your little counter, your whole personality for this object.

now here's the part that's actually kind of genius β€” every wait operation on the semaphore will atomically decrement the semaphore value, and it may potentially block if the semaphore value is 0. like, "no tickets left, hun, you're waiting in line." 😭

and every post operation will atomically increment the semaphore value, wake up the waiting threads, and let them retry the wait operation. so it's the reverse β€” somebody drops a ticket back in the bucket and the queue goes "ooh, me, me!" 🫢

not me writing fanfiction about a counter. anyway. atomic = no race conditions, no drama, no two threads stepping on each other. iykyk. πŸ’œ

πŸŽ€ FUNCTION PARAMETERS πŸŽ€

  • initial_value β€” the starting value of the semaphore. that's it. that's the arg. pick your number and go. ✨

πŸ‘‘ RETURN VALUE πŸ‘‘

returns a new semaphore, or NULL on failure. and if you get that NULL, bestie, do not just sit there β€” 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. every thread. all of them. no cap. πŸ”₯


⭐ AVAILABILITY ⭐

this function is available since SDL 3.2.0. so if you're on something older, respectfully, no. πŸ’€


πŸ’œ SEE ALSO πŸ’œ

your reading list, bestie, go be nosy:


and that's the whole page, bestie!! this one came straight out of Simple Directmedia Layer, version SDL 3.4.18, page SDL_CreateSemaphore(3). go make some semaphores and stay hydrated πŸ“πŸ’…βœ¨

πŸ“¦ where this page comes from