test/testhread.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 11 Jan 2009 04:05:28 +0000
changeset 3017 3272431eeee2
parent 2779 4436464c4f51
child 3578 0d1b16ee0bca
permissions -rw-r--r--
Added testresample.c
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@2779
    66
    signal(SIGTERM, killed);
slouken@1895
    67
    thread = SDL_CreateThread(ThreadFunc, "#2");
slouken@1895
    68
    if (thread == NULL) {
slouken@1895
    69
        fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
slouken@1895
    70
        quit(1);
slouken@1895
    71
    }
slouken@1895
    72
    raise(SIGTERM);
slouken@0
    73
slouken@1895
    74
    SDL_Quit();                 /* Never reached */
slouken@1895
    75
    return (0);                 /* Never reached */
slouken@0
    76
}