This is new wiki software and old wiki content. It's a work in progress!
Here's the explanation.
Be gentle, report bugs, leave feedback on pages, or just edit them yourself! Thanks!

SDL Wiki

SDL_atomic_t

A structure representing an atomic integer value.

Data Fields

int

value

the atomic integer value

Code Examples


/* This is a simple fps counter program
 *
 * Timers in SDL are run in a separate thread
 * so effort is needed to avoid data races
 */
SDL_atomic_t frames;

/* Calculate and display the average framerate over the set interval */
Uint32 fps_timer_callback(Uint32 interval, void *data)
{
    const float f = SDL_AtomicGet(&frames);
    const float iv = interval * 0.001f;

    /* Note: the thread safety of printf is ambiguous across platforms */
    printf("%.2f\tfps\n", f / iv);

    /* Reset frame counter */
    SDL_AtomicSet(&frames, 0);

    return interval;
}

int main(int argc, char **argv)
{
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_Window *win = SDL_CreateWindow("Counter",
            SDL_WINDOWPOS_CENTERED,
            SDL_WINDOWPOS_CENTERED,
            640,
            480,
            0
            );

    SDL_Surface *screen = SDL_GetWindowSurface(win);

    /* Our timer will be run every five seconds in a separate thread */
    SDL_AddTimer(5000, fps_timer_callback, NULL);

    SDL_Event e;
    while(1) {
        while (SDL_PollEvent(&e)) {
            if (e.type == SDL_QUIT) {
                goto quit;
            }
        }
        SDL_FillRect(screen, NULL, 0xffffffff);
        SDL_UpdateWindowSurface(win);

        /* Add one frame */
        SDL_AtomicAdd(&frames, 1);
    }
quit:
    SDL_DestroyWindow(win);
    SDL_Quit();
    return 0;
}

Remarks

SDL_atomic_t is a structure so people don't accidentally use numeric operations on it. Instead, you should use the atomic operation functions to work with the integer value.

SDL_AtomicAdd
SDL_AtomicCAS
SDL_AtomicDecRef
SDL_AtomicGet
SDL_AtomicIncRef
SDL_AtomicSet

CategoryStruct, CategoryAtomic


[ edit | delete | history | feedback | raw ]

[ front page | index | search | recent changes | git repo | offline html ]

All wiki content is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0).
Wiki powered by ghwikipp.