test/testthread.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 5508 60b63c32f06d
child 5969 3a041d215edc
permissions -rw-r--r--
SDL 1.3 is now under the zlib license.
slouken@5535
     1
/*
slouken@5535
     2
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
slouken@5535
     3
slouken@5535
     4
  This software is provided 'as-is', without any express or implied
slouken@5535
     5
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     6
  arising from the use of this software.
slouken@5535
     7
slouken@5535
     8
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
     9
  including commercial applications, and to alter it and redistribute it
slouken@5535
    10
  freely.
slouken@5535
    11
*/
slouken@0
    12
slouken@0
    13
/* Simple test of the SDL threading code */
slouken@0
    14
slouken@0
    15
#include <stdio.h>
slouken@0
    16
#include <stdlib.h>
slouken@0
    17
#include <signal.h>
slouken@0
    18
slouken@0
    19
#include "SDL.h"
slouken@0
    20
#include "SDL_thread.h"
slouken@0
    21
slouken@0
    22
static int alive = 0;
slouken@0
    23
icculus@1151
    24
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@1895
    25
static void
slouken@1895
    26
quit(int rc)
icculus@1151
    27
{
slouken@1895
    28
    SDL_Quit();
slouken@1895
    29
    exit(rc);
icculus@1151
    30
}
icculus@1151
    31
slouken@1895
    32
int SDLCALL
slouken@1895
    33
ThreadFunc(void *data)
slouken@0
    34
{
slouken@3578
    35
    printf("Started thread %s: My thread id is %lu\n",
slouken@1895
    36
           (char *) data, SDL_ThreadID());
slouken@1895
    37
    while (alive) {
slouken@1895
    38
        printf("Thread '%s' is alive!\n", (char *) data);
slouken@1895
    39
        SDL_Delay(1 * 1000);
slouken@1895
    40
    }
slouken@1895
    41
    printf("Thread '%s' exiting!\n", (char *) data);
slouken@1895
    42
    return (0);
slouken@0
    43
}
slouken@0
    44
slouken@1895
    45
static void
slouken@1895
    46
killed(int sig)
slouken@0
    47
{
slouken@1895
    48
    printf("Killed with SIGTERM, waiting 5 seconds to exit\n");
slouken@1895
    49
    SDL_Delay(5 * 1000);
slouken@1895
    50
    alive = 0;
slouken@1895
    51
    quit(0);
slouken@0
    52
}
slouken@0
    53
slouken@1895
    54
int
slouken@1895
    55
main(int argc, char *argv[])
slouken@0
    56
{
slouken@1895
    57
    SDL_Thread *thread;
slouken@0
    58
slouken@1895
    59
    /* Load the SDL library */
slouken@1895
    60
    if (SDL_Init(0) < 0) {
slouken@1895
    61
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@1895
    62
        return (1);
slouken@1895
    63
    }
slouken@0
    64
slouken@1895
    65
    alive = 1;
slouken@1895
    66
    thread = SDL_CreateThread(ThreadFunc, "#1");
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("Waiting for thread #1\n");
slouken@1895
    73
    alive = 0;
slouken@1895
    74
    SDL_WaitThread(thread, NULL);
slouken@0
    75
slouken@1895
    76
    alive = 1;
slouken@2779
    77
    signal(SIGTERM, killed);
slouken@1895
    78
    thread = SDL_CreateThread(ThreadFunc, "#2");
slouken@1895
    79
    if (thread == NULL) {
slouken@1895
    80
        fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
slouken@1895
    81
        quit(1);
slouken@1895
    82
    }
slouken@1895
    83
    raise(SIGTERM);
slouken@0
    84
slouken@1895
    85
    SDL_Quit();                 /* Never reached */
slouken@1895
    86
    return (0);                 /* Never reached */
slouken@0
    87
}