test/testhread.c
changeset 0 74212992fb08
child 1151 be9c9c8f6d53
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testhread.c	Thu Apr 26 16:45:43 2001 +0000
     1.3 @@ -0,0 +1,75 @@
     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 +
    1.11 +#include "SDL.h"
    1.12 +#include "SDL_thread.h"
    1.13 +
    1.14 +static int alive = 0;
    1.15 +
    1.16 +int ThreadFunc(void *data)
    1.17 +{
    1.18 +	printf("Started thread %s: My thread id is %u\n",
    1.19 +				(char *)data, SDL_ThreadID());
    1.20 +	while ( alive ) {
    1.21 +		printf("Thread '%s' is alive!\n", (char *)data);
    1.22 +		SDL_Delay(1*1000);
    1.23 +	}
    1.24 +	printf("Thread '%s' exiting!\n", (char *)data);
    1.25 +	return(0);
    1.26 +}
    1.27 +
    1.28 +static void killed(int sig)
    1.29 +{
    1.30 +	printf("Killed with SIGTERM, waiting 5 seconds to exit\n");
    1.31 +	SDL_Delay(5*1000);
    1.32 +	alive = 0;
    1.33 +	exit(0);
    1.34 +}
    1.35 +
    1.36 +int main(int argc, char *argv[])
    1.37 +{
    1.38 +	SDL_Thread *thread;
    1.39 +
    1.40 +	/* Load the SDL library */
    1.41 +	if ( SDL_Init(0) < 0 ) {
    1.42 +		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
    1.43 +		exit(1);
    1.44 +	}
    1.45 +	atexit(SDL_Quit);
    1.46 +
    1.47 +	alive = 1;
    1.48 +	thread = SDL_CreateThread(ThreadFunc, "#1");
    1.49 +	if ( thread == NULL ) {
    1.50 +		fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
    1.51 +		exit(1);
    1.52 +	}
    1.53 +	SDL_Delay(5*1000);
    1.54 +	printf("Waiting for thread #1\n");
    1.55 +	alive = 0;
    1.56 +	SDL_WaitThread(thread, NULL);
    1.57 +
    1.58 +	alive = 1;
    1.59 +	thread = SDL_CreateThread(ThreadFunc, "#2");
    1.60 +	if ( thread == NULL ) {
    1.61 +		fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
    1.62 +		exit(1);
    1.63 +	}
    1.64 +	SDL_Delay(5*1000);
    1.65 +	printf("Killing thread #2\n");
    1.66 +	SDL_KillThread(thread);
    1.67 +
    1.68 +	alive = 1;
    1.69 +	signal(SIGTERM, killed);
    1.70 +	thread = SDL_CreateThread(ThreadFunc, "#3");
    1.71 +	if ( thread == NULL ) {
    1.72 +		fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
    1.73 +		exit(1);
    1.74 +	}
    1.75 +	raise(SIGTERM);
    1.76 +
    1.77 +	return(0);	/* Never reached */
    1.78 +}