test/torturethread.c
changeset 0 74212992fb08
child 1151 be9c9c8f6d53
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/torturethread.c	Thu Apr 26 16:45:43 2001 +0000
     1.3 @@ -0,0 +1,85 @@
     1.4 +
     1.5 +/* Simple test of the SDL threading code */
     1.6 +
     1.7 +#include <stdio.h>
     1.8 +#include <stdlib.h>
     1.9 +#include <signal.h>
    1.10 +#include <string.h>
    1.11 +
    1.12 +#include "SDL.h"
    1.13 +#include "SDL_thread.h"
    1.14 +
    1.15 +#define NUMTHREADS 10
    1.16 +
    1.17 +static char volatile time_for_threads_to_die[NUMTHREADS];
    1.18 +
    1.19 +int SubThreadFunc(void *data) {
    1.20 +	while(! *(int volatile *)data) {
    1.21 +		; /*SDL_Delay(10); /* do nothing */
    1.22 +	}
    1.23 +	return 0;
    1.24 +}
    1.25 +
    1.26 +int ThreadFunc(void *data) {
    1.27 +	SDL_Thread *sub_threads[NUMTHREADS];
    1.28 +	int flags[NUMTHREADS];
    1.29 +	int i;
    1.30 +	int tid = (int ) data;
    1.31 +
    1.32 +	fprintf(stderr, "Creating Thread %d\n", tid);
    1.33 +
    1.34 +	for(i = 0; i < NUMTHREADS; i++) {
    1.35 +		flags[i] = 0;
    1.36 +		sub_threads[i] = SDL_CreateThread(SubThreadFunc, &flags[i]);
    1.37 +	}
    1.38 +
    1.39 +	printf("Thread '%d' waiting for signal\n", tid);
    1.40 +	while(time_for_threads_to_die[tid] != 1) {
    1.41 +		; /* do nothing */
    1.42 +	}
    1.43 +
    1.44 +	printf("Thread '%d' sending signals to subthreads\n", tid);
    1.45 +	for(i = 0; i <  NUMTHREADS; i++) {
    1.46 +		flags[i] = 1;
    1.47 +		SDL_WaitThread(sub_threads[i], NULL);
    1.48 +	}
    1.49 +
    1.50 +	printf("Thread '%d' exiting!\n", tid);
    1.51 +
    1.52 +	return 0;
    1.53 +}
    1.54 +
    1.55 +int main(int argc, char *argv[])
    1.56 +{
    1.57 +	SDL_Thread *threads[NUMTHREADS];
    1.58 +	int i;
    1.59 +
    1.60 +	/* Load the SDL library */
    1.61 +	if ( SDL_Init(0) < 0 ) {
    1.62 +		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
    1.63 +		exit(1);
    1.64 +	}
    1.65 +	atexit(SDL_Quit);
    1.66 +
    1.67 +
    1.68 +	signal(SIGSEGV, SIG_DFL);
    1.69 +	for(i = 0; i < NUMTHREADS; i++) {
    1.70 +		time_for_threads_to_die[i] = 0;
    1.71 +		threads[i] = SDL_CreateThread(ThreadFunc, (void *) i);
    1.72 +	
    1.73 +		if ( threads[i] == NULL ) {
    1.74 +			fprintf(stderr,
    1.75 +			"Couldn't create thread: %s\n", SDL_GetError());
    1.76 +			exit(1);
    1.77 +		}
    1.78 +	}
    1.79 +
    1.80 +	for(i = 0; i < NUMTHREADS; i++) {
    1.81 +		time_for_threads_to_die[i] = 1;
    1.82 +	}
    1.83 +
    1.84 +	for(i = NUMTHREADS-1; i >= 0; --i) {
    1.85 +		SDL_WaitThread(threads[i], NULL);
    1.86 +	}
    1.87 +	return(0);	/* Never reached */
    1.88 +}