2 /* Simple test of the SDL threading code */
10 #include "SDL_thread.h"
14 static char volatile time_for_threads_to_die[NUMTHREADS];
16 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
17 static void quit(int rc)
23 int SDLCALL SubThreadFunc(void *data) {
24 while(! *(int volatile *)data) {
25 ; /*SDL_Delay(10);*/ /* do nothing */
30 int SDLCALL ThreadFunc(void *data) {
31 SDL_Thread *sub_threads[NUMTHREADS];
32 int flags[NUMTHREADS];
34 int tid = (int)(uintptr_t)data;
36 fprintf(stderr, "Creating Thread %d\n", tid);
38 for(i = 0; i < NUMTHREADS; i++) {
40 sub_threads[i] = SDL_CreateThread(SubThreadFunc, &flags[i]);
43 printf("Thread '%d' waiting for signal\n", tid);
44 while(time_for_threads_to_die[tid] != 1) {
48 printf("Thread '%d' sending signals to subthreads\n", tid);
49 for(i = 0; i < NUMTHREADS; i++) {
51 SDL_WaitThread(sub_threads[i], NULL);
54 printf("Thread '%d' exiting!\n", tid);
59 int main(int argc, char *argv[])
61 SDL_Thread *threads[NUMTHREADS];
64 /* Load the SDL library */
65 if ( SDL_Init(0) < 0 ) {
66 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
70 signal(SIGSEGV, SIG_DFL);
71 for(i = 0; i < NUMTHREADS; i++) {
72 time_for_threads_to_die[i] = 0;
73 threads[i] = SDL_CreateThread(ThreadFunc, (void *)(uintptr_t)i);
75 if ( threads[i] == NULL ) {
77 "Couldn't create thread: %s\n", SDL_GetError());
82 for(i = 0; i < NUMTHREADS; i++) {
83 time_for_threads_to_die[i] = 1;
86 for(i = 0; i < NUMTHREADS; i++) {
87 SDL_WaitThread(threads[i], NULL);