0
|
1
|
|
2 /* Simple test of the SDL threading code */
|
|
3
|
|
4 #include <stdio.h>
|
|
5 #include <stdlib.h>
|
|
6 #include <signal.h>
|
|
7 #include <string.h>
|
|
8
|
|
9 #include "SDL.h"
|
|
10 #include "SDL_thread.h"
|
|
11
|
|
12 #define NUMTHREADS 10
|
|
13
|
|
14 static char volatile time_for_threads_to_die[NUMTHREADS];
|
|
15
|
|
16 int SubThreadFunc(void *data) {
|
|
17 while(! *(int volatile *)data) {
|
|
18 ; /*SDL_Delay(10); /* do nothing */
|
|
19 }
|
|
20 return 0;
|
|
21 }
|
|
22
|
|
23 int ThreadFunc(void *data) {
|
|
24 SDL_Thread *sub_threads[NUMTHREADS];
|
|
25 int flags[NUMTHREADS];
|
|
26 int i;
|
|
27 int tid = (int ) data;
|
|
28
|
|
29 fprintf(stderr, "Creating Thread %d\n", tid);
|
|
30
|
|
31 for(i = 0; i < NUMTHREADS; i++) {
|
|
32 flags[i] = 0;
|
|
33 sub_threads[i] = SDL_CreateThread(SubThreadFunc, &flags[i]);
|
|
34 }
|
|
35
|
|
36 printf("Thread '%d' waiting for signal\n", tid);
|
|
37 while(time_for_threads_to_die[tid] != 1) {
|
|
38 ; /* do nothing */
|
|
39 }
|
|
40
|
|
41 printf("Thread '%d' sending signals to subthreads\n", tid);
|
|
42 for(i = 0; i < NUMTHREADS; i++) {
|
|
43 flags[i] = 1;
|
|
44 SDL_WaitThread(sub_threads[i], NULL);
|
|
45 }
|
|
46
|
|
47 printf("Thread '%d' exiting!\n", tid);
|
|
48
|
|
49 return 0;
|
|
50 }
|
|
51
|
|
52 int main(int argc, char *argv[])
|
|
53 {
|
|
54 SDL_Thread *threads[NUMTHREADS];
|
|
55 int i;
|
|
56
|
|
57 /* Load the SDL library */
|
|
58 if ( SDL_Init(0) < 0 ) {
|
|
59 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
|
|
60 exit(1);
|
|
61 }
|
|
62 atexit(SDL_Quit);
|
|
63
|
|
64
|
|
65 signal(SIGSEGV, SIG_DFL);
|
|
66 for(i = 0; i < NUMTHREADS; i++) {
|
|
67 time_for_threads_to_die[i] = 0;
|
|
68 threads[i] = SDL_CreateThread(ThreadFunc, (void *) i);
|
|
69
|
|
70 if ( threads[i] == NULL ) {
|
|
71 fprintf(stderr,
|
|
72 "Couldn't create thread: %s\n", SDL_GetError());
|
|
73 exit(1);
|
|
74 }
|
|
75 }
|
|
76
|
|
77 for(i = 0; i < NUMTHREADS; i++) {
|
|
78 time_for_threads_to_die[i] = 1;
|
|
79 }
|
|
80
|
|
81 for(i = NUMTHREADS-1; i >= 0; --i) {
|
|
82 SDL_WaitThread(threads[i], NULL);
|
|
83 }
|
|
84 return(0); /* Never reached */
|
|
85 }
|