SDL_InitState(3type)

🫢 SDL_InitState 🫢

okay bestie, welcome to the SDL3 DATATYPES corner of the manual, where we're talking about SDL_InitState β€” the little struct that lets you do thread-safe initialization and shutdown. yes queen, concurrency, we love her πŸ’…


✨ NAME ✨

SDL_InitState β€” a structure used for thread-safe initialization and shutdown. that's it, that's the whole vibe πŸ¦‹


🌸 SYNOPSIS 🌸

#include <SDL3/SDL_mutex.h>

typedef struct SDL_InitState
{
    SDL_AtomicInt status;
    SDL_ThreadID thread;
    void *reserved;
} SDL_InitState;

look at her. three fields. status, thread, reserved. don't touch reserved babe, that one's not for you πŸ™ˆ


πŸ’– DESCRIPTION πŸ’–

here's an example of using this, and honestly? it's giving textbook ✨

static SDL_InitState init;

bool InitSystem(void)
{
    if (!SDL_ShouldInit(&init)) {
        // The system is initialized
        return true;
    }

    // At this point, you should not leave this function without calling SDL_SetInitialized()

    bool initialized = DoInitTasks();
    SDL_SetInitialized(&init, initialized);
    return initialized;
}

bool UseSubsystem(void)
{
    if (SDL_ShouldInit(&init)) {
        // Error, the subsystem isn't initialized
        SDL_SetInitialized(&init, false);
        return false;
    }

    // Do work using the initialized subsystem

    return true;
}

void QuitSystem(void)
{
    if (!SDL_ShouldQuit(&init)) {
        // The system is not initialized
        return;
    }

    // At this point, you should not leave this function without calling SDL_SetInitialized()

    DoQuitTasks();
    SDL_SetInitialized(&init, false);
}

so like, read the comments fr fr β€” you should not leave this function without calling SDL_SetInitialized(). that part is in the source comments twice, so it's not a suggestion, it's the rules bestie πŸŽ€

⚠️ okay important, no cap: this doesn't protect any resources created during initialization, and it doesn't guarantee that nobody is using those resources during cleanup. you should use other mechanisms to protect those, if that's a concern for your code. the struct handles the init/shutdown handshake, not your whole life πŸ’€

🌈 AVAILABILITY 🌈

this struct is available since SDL 3.2.0. she's new-ish, she's fresh, she ate πŸ‘‘


and that's the whole page, bestie 🫢 this was SDL_InitState(3type), from the Simple Directmedia Layer era of SDL 3.4.18 β€” go forth and initialize things thread-safely, mwah πŸ’–βœ¨

πŸ“¦ where this page comes from