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