test/testsem.c
changeset 1895 c121d94672cb
parent 1769 290b5baf2fca
child 5106 d547877e355e
equal deleted inserted replaced
1894:c69cee13dd76 1895:c121d94672cb
    11 #define NUM_THREADS 10
    11 #define NUM_THREADS 10
    12 
    12 
    13 static SDL_sem *sem;
    13 static SDL_sem *sem;
    14 int alive = 1;
    14 int alive = 1;
    15 
    15 
    16 int SDLCALL ThreadFunc(void *data)
    16 int SDLCALL
       
    17 ThreadFunc(void *data)
    17 {
    18 {
    18 	int threadnum = (int)(uintptr_t)data;
    19     int threadnum = (int) (uintptr_t) data;
    19 	while ( alive ) {
    20     while (alive) {
    20 		SDL_SemWait(sem);
    21         SDL_SemWait(sem);
    21 		fprintf(stderr, "Thread number %d has got the semaphore (value = %d)!\n", threadnum, SDL_SemValue(sem));
    22         fprintf(stderr,
    22 		SDL_Delay(200);
    23                 "Thread number %d has got the semaphore (value = %d)!\n",
    23 		SDL_SemPost(sem);
    24                 threadnum, SDL_SemValue(sem));
    24 		fprintf(stderr, "Thread number %d has released the semaphore (value = %d)!\n", threadnum, SDL_SemValue(sem));
    25         SDL_Delay(200);
    25 		SDL_Delay(1); /* For the scheduler */
    26         SDL_SemPost(sem);
    26 	}
    27         fprintf(stderr,
    27 	printf("Thread number %d exiting.\n", threadnum);
    28                 "Thread number %d has released the semaphore (value = %d)!\n",
    28 	return 0;
    29                 threadnum, SDL_SemValue(sem));
       
    30         SDL_Delay(1);           /* For the scheduler */
       
    31     }
       
    32     printf("Thread number %d exiting.\n", threadnum);
       
    33     return 0;
    29 }
    34 }
    30 
    35 
    31 static void killed(int sig)
    36 static void
       
    37 killed(int sig)
    32 {
    38 {
    33 	alive = 0;
    39     alive = 0;
    34 }
    40 }
    35 
    41 
    36 int main(int argc, char **argv)
    42 int
       
    43 main(int argc, char **argv)
    37 {
    44 {
    38 	SDL_Thread *threads[NUM_THREADS];
    45     SDL_Thread *threads[NUM_THREADS];
    39 	uintptr_t i;
    46     uintptr_t i;
    40 	int init_sem;
    47     int init_sem;
    41 
    48 
    42 	if(argc < 2) {
    49     if (argc < 2) {
    43 		fprintf(stderr,"Usage: %s init_value\n", argv[0]);
    50         fprintf(stderr, "Usage: %s init_value\n", argv[0]);
    44 		return(1);
    51         return (1);
    45 	}
    52     }
    46 
    53 
    47 	/* Load the SDL library */
    54     /* Load the SDL library */
    48 	if ( SDL_Init(0) < 0 ) {
    55     if (SDL_Init(0) < 0) {
    49 		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
    56         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
    50 		return(1);
    57         return (1);
    51 	}
    58     }
    52 	signal(SIGTERM, killed);
    59     signal(SIGTERM, killed);
    53 	signal(SIGINT, killed);
    60     signal(SIGINT, killed);
    54 	
       
    55 	init_sem = atoi(argv[1]);
       
    56 	sem = SDL_CreateSemaphore(init_sem);
       
    57 	
       
    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);
       
    62 	}
       
    63 
    61 
    64 	/* Wait 10 seconds */
    62     init_sem = atoi(argv[1]);
    65 	SDL_Delay(10 * 1000);
    63     sem = SDL_CreateSemaphore(init_sem);
    66 
    64 
    67 	/* Wait for all threads to finish */
    65     printf("Running %d threads, semaphore value = %d\n", NUM_THREADS,
    68 	printf("Waiting for threads to finish\n");
    66            init_sem);
    69 	alive = 0;
    67     /* Create all the threads */
    70 	for( i = 0; i < NUM_THREADS; ++i ) {
    68     for (i = 0; i < NUM_THREADS; ++i) {
    71 		SDL_WaitThread(threads[i], NULL);
    69         threads[i] = SDL_CreateThread(ThreadFunc, (void *) i);
    72 	}
    70     }
    73 	printf("Finished waiting for threads\n");
       
    74 
    71 
    75 	SDL_DestroySemaphore(sem);
    72     /* Wait 10 seconds */
    76 	SDL_Quit();
    73     SDL_Delay(10 * 1000);
    77 	return(0);
    74 
       
    75     /* Wait for all threads to finish */
       
    76     printf("Waiting for threads to finish\n");
       
    77     alive = 0;
       
    78     for (i = 0; i < NUM_THREADS; ++i) {
       
    79         SDL_WaitThread(threads[i], NULL);
       
    80     }
       
    81     printf("Finished waiting for threads\n");
       
    82 
       
    83     SDL_DestroySemaphore(sem);
       
    84     SDL_Quit();
       
    85     return (0);
    78 }
    86 }