2 /* Simple test of the SDL semaphore code */
9 #include "SDL_thread.h"
11 #define NUM_THREADS 10
16 int SDLCALL
ThreadFunc(void *data
)
18 int threadnum
= (int)(uintptr_t)data
;
21 fprintf(stderr
, "Thread number %d has got the semaphore (value = %d)!\n", threadnum
, SDL_SemValue(sem
));
24 fprintf(stderr
, "Thread number %d has released the semaphore (value = %d)!\n", threadnum
, SDL_SemValue(sem
));
25 SDL_Delay(1); /* For the scheduler */
27 printf("Thread number %d exiting.\n", threadnum
);
31 static void killed(int sig
)
36 int main(int argc
, char **argv
)
38 SDL_Thread
*threads
[NUM_THREADS
];
43 fprintf(stderr
,"Usage: %s init_value\n", argv
[0]);
47 /* Load the SDL library */
48 if ( SDL_Init(0) < 0 ) {
49 fprintf(stderr
, "Couldn't initialize SDL: %s\n",SDL_GetError());
52 signal(SIGTERM
, killed
);
53 signal(SIGINT
, killed
);
55 init_sem
= atoi(argv
[1]);
56 sem
= SDL_CreateSemaphore(init_sem
);
58 printf("Running %d threads, semaphore value = %d\n", NUM_THREADS
, init_sem
);
59 /* Create all the threads */
60 for( i
= 0; i
< NUM_THREADS
; ++i
) {
61 threads
[i
] = SDL_CreateThread(ThreadFunc
, (void*)i
);
67 /* Wait for all threads to finish */
68 printf("Waiting for threads to finish\n");
70 for( i
= 0; i
< NUM_THREADS
; ++i
) {
71 SDL_WaitThread(threads
[i
], NULL
);
73 printf("Finished waiting for threads\n");
75 SDL_DestroySemaphore(sem
);