test/testthread.c
changeset 5508 60b63c32f06d
parent 3578 0d1b16ee0bca
child 5535 96594ac5fd1a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testthread.c	Fri Mar 25 11:09:57 2011 -0700
     1.3 @@ -0,0 +1,76 @@
     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 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    1.17 +static void
    1.18 +quit(int rc)
    1.19 +{
    1.20 +    SDL_Quit();
    1.21 +    exit(rc);
    1.22 +}
    1.23 +
    1.24 +int SDLCALL
    1.25 +ThreadFunc(void *data)
    1.26 +{
    1.27 +    printf("Started thread %s: My thread id is %lu\n",
    1.28 +           (char *) data, SDL_ThreadID());
    1.29 +    while (alive) {
    1.30 +        printf("Thread '%s' is alive!\n", (char *) data);
    1.31 +        SDL_Delay(1 * 1000);
    1.32 +    }
    1.33 +    printf("Thread '%s' exiting!\n", (char *) data);
    1.34 +    return (0);
    1.35 +}
    1.36 +
    1.37 +static void
    1.38 +killed(int sig)
    1.39 +{
    1.40 +    printf("Killed with SIGTERM, waiting 5 seconds to exit\n");
    1.41 +    SDL_Delay(5 * 1000);
    1.42 +    alive = 0;
    1.43 +    quit(0);
    1.44 +}
    1.45 +
    1.46 +int
    1.47 +main(int argc, char *argv[])
    1.48 +{
    1.49 +    SDL_Thread *thread;
    1.50 +
    1.51 +    /* Load the SDL library */
    1.52 +    if (SDL_Init(0) < 0) {
    1.53 +        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
    1.54 +        return (1);
    1.55 +    }
    1.56 +
    1.57 +    alive = 1;
    1.58 +    thread = SDL_CreateThread(ThreadFunc, "#1");
    1.59 +    if (thread == NULL) {
    1.60 +        fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
    1.61 +        quit(1);
    1.62 +    }
    1.63 +    SDL_Delay(5 * 1000);
    1.64 +    printf("Waiting for thread #1\n");
    1.65 +    alive = 0;
    1.66 +    SDL_WaitThread(thread, NULL);
    1.67 +
    1.68 +    alive = 1;
    1.69 +    signal(SIGTERM, killed);
    1.70 +    thread = SDL_CreateThread(ThreadFunc, "#2");
    1.71 +    if (thread == NULL) {
    1.72 +        fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
    1.73 +        quit(1);
    1.74 +    }
    1.75 +    raise(SIGTERM);
    1.76 +
    1.77 +    SDL_Quit();                 /* Never reached */
    1.78 +    return (0);                 /* Never reached */
    1.79 +}