A structure representing an atomic integer value.
int |
value |
the atomic integer value |
/* 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 */
void *data)
Uint32 fps_timer_callback(Uint32 interval,
{const float f = SDL_AtomicGet(&frames);
const float iv = interval * 0.001f;
/* Note: the thread safety of printf is ambiguous across platforms */
"%.2f\tfps\n", f / iv);
printf(
/* Reset frame counter */
0);
SDL_AtomicSet(&frames,
return interval;
}
int main(int argc, char **argv)
{
SDL_Init(SDL_INIT_EVERYTHING);"Counter",
SDL_Window *win = SDL_CreateWindow(
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 */
5000, fps_timer_callback, NULL);
SDL_AddTimer(
SDL_Event e;while(1) {
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
goto quit;
}
}0xffffffff);
SDL_FillRect(screen, NULL,
SDL_UpdateWindowSurface(win);
/* Add one frame */
1);
SDL_AtomicAdd(&frames,
}
quit:
SDL_DestroyWindow(win);
SDL_Quit();return 0;
}
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.
CategoryStruct, CategoryAtomic