test/testerror.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 and error handling */
slouken@0
     3
slouken@0
     4
#include <stdio.h>
slouken@0
     5
#include <stdlib.h>
slouken@0
     6
#include <signal.h>
slouken@0
     7
slouken@0
     8
#include "SDL.h"
slouken@0
     9
#include "SDL_thread.h"
slouken@0
    10
slouken@0
    11
static int alive = 0;
slouken@0
    12
slouken@0
    13
int ThreadFunc(void *data)
slouken@0
    14
{
slouken@0
    15
	/* Set the child thread error string */
slouken@0
    16
	SDL_SetError("Thread %s (%d) had a problem: %s",
slouken@0
    17
			(char *)data, SDL_ThreadID(), "nevermind");
slouken@0
    18
	while ( alive ) {
slouken@0
    19
		printf("Thread '%s' is alive!\n", (char *)data);
slouken@0
    20
		SDL_Delay(1*1000);
slouken@0
    21
	}
slouken@0
    22
	printf("Child thread error string: %s\n", SDL_GetError());
slouken@0
    23
	return(0);
slouken@0
    24
}
slouken@0
    25
slouken@0
    26
int main(int argc, char *argv[])
slouken@0
    27
{
slouken@0
    28
	SDL_Thread *thread;
slouken@0
    29
slouken@0
    30
	/* Load the SDL library */
slouken@0
    31
	if ( SDL_Init(0) < 0 ) {
slouken@0
    32
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
slouken@0
    33
		exit(1);
slouken@0
    34
	}
slouken@0
    35
	atexit(SDL_Quit);
slouken@0
    36
slouken@0
    37
	/* Set the error value for the main thread */
slouken@0
    38
	SDL_SetError("No worries");
slouken@0
    39
slouken@0
    40
	alive = 1;
slouken@0
    41
	thread = SDL_CreateThread(ThreadFunc, "#1");
slouken@0
    42
	if ( thread == NULL ) {
slouken@0
    43
		fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
slouken@0
    44
		exit(1);
slouken@0
    45
	}
slouken@0
    46
	SDL_Delay(5*1000);
slouken@0
    47
	printf("Waiting for thread #1\n");
slouken@0
    48
	alive = 0;
slouken@0
    49
	SDL_WaitThread(thread, NULL);
slouken@0
    50
slouken@0
    51
	printf("Main thread error string: %s\n", SDL_GetError());
slouken@0
    52
slouken@0
    53
	return(0);
slouken@0
    54
}