test/testhread.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 09 May 2006 07:26:58 +0000
changeset 1790 828a17e05192
parent 1769 290b5baf2fca
child 1662 782fd950bd46
child 1895 c121d94672cb
permissions -rw-r--r--
Date: Mon, 8 May 2006 14:19:30 -0700
From: Bob Ippolito
Subject: SDL trunk (r2346) and Mac OS X

The current state of the trunk doesn't quite compile on Mac OS X,
I've attached a series of patches that gets it to compile and kills a
few warnings.

sdl-trunk-r2346-dlcompat-warnings.diff:
The dlcompat thing is just loaded with incorrect type signatures ..
some of them have changed since 10.2 and others are just flat wrong.
This puts it in sync with the latest headers. People on 10.2 will get
the warnings instead of people with new kits.
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
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
icculus@1151
    13
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
icculus@1151
    14
static void quit(int rc)
icculus@1151
    15
{
icculus@1151
    16
	SDL_Quit();
icculus@1151
    17
	exit(rc);
icculus@1151
    18
}
icculus@1151
    19
slouken@1769
    20
int SDLCALL ThreadFunc(void *data)
slouken@0
    21
{
slouken@0
    22
	printf("Started thread %s: My thread id is %u\n",
slouken@0
    23
				(char *)data, SDL_ThreadID());
slouken@0
    24
	while ( alive ) {
slouken@0
    25
		printf("Thread '%s' is alive!\n", (char *)data);
slouken@0
    26
		SDL_Delay(1*1000);
slouken@0
    27
	}
slouken@0
    28
	printf("Thread '%s' exiting!\n", (char *)data);
slouken@0
    29
	return(0);
slouken@0
    30
}
slouken@0
    31
slouken@0
    32
static void killed(int sig)
slouken@0
    33
{
slouken@0
    34
	printf("Killed with SIGTERM, waiting 5 seconds to exit\n");
slouken@0
    35
	SDL_Delay(5*1000);
slouken@0
    36
	alive = 0;
icculus@1151
    37
	quit(0);
slouken@0
    38
}
slouken@0
    39
slouken@0
    40
int main(int argc, char *argv[])
slouken@0
    41
{
slouken@0
    42
	SDL_Thread *thread;
slouken@0
    43
slouken@0
    44
	/* Load the SDL library */
slouken@0
    45
	if ( SDL_Init(0) < 0 ) {
slouken@0
    46
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
icculus@1151
    47
		return(1);
slouken@0
    48
	}
slouken@0
    49
slouken@0
    50
	alive = 1;
slouken@0
    51
	thread = SDL_CreateThread(ThreadFunc, "#1");
slouken@0
    52
	if ( thread == NULL ) {
slouken@0
    53
		fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
icculus@1151
    54
		quit(1);
slouken@0
    55
	}
slouken@0
    56
	SDL_Delay(5*1000);
slouken@0
    57
	printf("Waiting for thread #1\n");
slouken@0
    58
	alive = 0;
slouken@0
    59
	SDL_WaitThread(thread, NULL);
slouken@0
    60
slouken@0
    61
	alive = 1;
slouken@0
    62
	thread = SDL_CreateThread(ThreadFunc, "#2");
slouken@0
    63
	if ( thread == NULL ) {
slouken@0
    64
		fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
icculus@1151
    65
		quit(1);
slouken@0
    66
	}
slouken@0
    67
	SDL_Delay(5*1000);
slouken@0
    68
	printf("Killing thread #2\n");
slouken@0
    69
	SDL_KillThread(thread);
slouken@0
    70
slouken@0
    71
	alive = 1;
slouken@0
    72
	signal(SIGTERM, killed);
slouken@0
    73
	thread = SDL_CreateThread(ThreadFunc, "#3");
slouken@0
    74
	if ( thread == NULL ) {
slouken@0
    75
		fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
icculus@1151
    76
		quit(1);
slouken@0
    77
	}
slouken@0
    78
	raise(SIGTERM);
slouken@0
    79
icculus@1151
    80
	SDL_Quit();	/* Never reached */
slouken@0
    81
	return(0);	/* Never reached */
slouken@0
    82
}