test/testhread.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 10 Jul 2006 21:04:37 +0000
changeset 1895 c121d94672cb
parent 1769 290b5baf2fca
child 2779 4436464c4f51
permissions -rw-r--r--
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
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. */
slouken@1895
    14
static void
slouken@1895
    15
quit(int rc)
icculus@1151
    16
{
slouken@1895
    17
    SDL_Quit();
slouken@1895
    18
    exit(rc);
icculus@1151
    19
}
icculus@1151
    20
slouken@1895
    21
int SDLCALL
slouken@1895
    22
ThreadFunc(void *data)
slouken@0
    23
{
slouken@1895
    24
    printf("Started thread %s: My thread id is %u\n",
slouken@1895
    25
           (char *) data, SDL_ThreadID());
slouken@1895
    26
    while (alive) {
slouken@1895
    27
        printf("Thread '%s' is alive!\n", (char *) data);
slouken@1895
    28
        SDL_Delay(1 * 1000);
slouken@1895
    29
    }
slouken@1895
    30
    printf("Thread '%s' exiting!\n", (char *) data);
slouken@1895
    31
    return (0);
slouken@0
    32
}
slouken@0
    33
slouken@1895
    34
static void
slouken@1895
    35
killed(int sig)
slouken@0
    36
{
slouken@1895
    37
    printf("Killed with SIGTERM, waiting 5 seconds to exit\n");
slouken@1895
    38
    SDL_Delay(5 * 1000);
slouken@1895
    39
    alive = 0;
slouken@1895
    40
    quit(0);
slouken@0
    41
}
slouken@0
    42
slouken@1895
    43
int
slouken@1895
    44
main(int argc, char *argv[])
slouken@0
    45
{
slouken@1895
    46
    SDL_Thread *thread;
slouken@0
    47
slouken@1895
    48
    /* Load the SDL library */
slouken@1895
    49
    if (SDL_Init(0) < 0) {
slouken@1895
    50
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@1895
    51
        return (1);
slouken@1895
    52
    }
slouken@0
    53
slouken@1895
    54
    alive = 1;
slouken@1895
    55
    thread = SDL_CreateThread(ThreadFunc, "#1");
slouken@1895
    56
    if (thread == NULL) {
slouken@1895
    57
        fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
slouken@1895
    58
        quit(1);
slouken@1895
    59
    }
slouken@1895
    60
    SDL_Delay(5 * 1000);
slouken@1895
    61
    printf("Waiting for thread #1\n");
slouken@1895
    62
    alive = 0;
slouken@1895
    63
    SDL_WaitThread(thread, NULL);
slouken@0
    64
slouken@1895
    65
    alive = 1;
slouken@1895
    66
    thread = SDL_CreateThread(ThreadFunc, "#2");
slouken@1895
    67
    if (thread == NULL) {
slouken@1895
    68
        fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
slouken@1895
    69
        quit(1);
slouken@1895
    70
    }
slouken@1895
    71
    SDL_Delay(5 * 1000);
slouken@1895
    72
    printf("Killing thread #2\n");
slouken@1895
    73
    SDL_KillThread(thread);
slouken@0
    74
slouken@1895
    75
    alive = 1;
slouken@1895
    76
    signal(SIGTERM, killed);
slouken@1895
    77
    thread = SDL_CreateThread(ThreadFunc, "#3");
slouken@1895
    78
    if (thread == NULL) {
slouken@1895
    79
        fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
slouken@1895
    80
        quit(1);
slouken@1895
    81
    }
slouken@1895
    82
    raise(SIGTERM);
slouken@0
    83
slouken@1895
    84
    SDL_Quit();                 /* Never reached */
slouken@1895
    85
    return (0);                 /* Never reached */
slouken@0
    86
}