test/testhread.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 28 May 2006 13:04:16 +0000
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1659 14717b52abc0
child 1668 4da1ee79c9af
permissions -rw-r--r--
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.

WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid.

The code is now run through a consistent indent format:
indent -i4 -nut -nsc -br -ce

The headers are being converted to automatically generate doxygen documentation.
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@1662
    14
static void
slouken@1662
    15
quit (int rc)
icculus@1151
    16
{
slouken@1662
    17
    SDL_Quit ();
slouken@1662
    18
    exit (rc);
icculus@1151
    19
}
icculus@1151
    20
slouken@1662
    21
int SDLCALL
slouken@1662
    22
ThreadFunc (void *data)
slouken@0
    23
{
slouken@1662
    24
    printf ("Started thread %s: My thread id is %u\n",
slouken@1662
    25
            (char *) data, SDL_ThreadID ());
slouken@1662
    26
    while (alive) {
slouken@1662
    27
        printf ("Thread '%s' is alive!\n", (char *) data);
slouken@1662
    28
        SDL_Delay (1 * 1000);
slouken@1662
    29
    }
slouken@1662
    30
    printf ("Thread '%s' exiting!\n", (char *) data);
slouken@1662
    31
    return (0);
slouken@0
    32
}
slouken@0
    33
slouken@1662
    34
static void
slouken@1662
    35
killed (int sig)
slouken@0
    36
{
slouken@1662
    37
    printf ("Killed with SIGTERM, waiting 5 seconds to exit\n");
slouken@1662
    38
    SDL_Delay (5 * 1000);
slouken@1662
    39
    alive = 0;
slouken@1662
    40
    quit (0);
slouken@0
    41
}
slouken@0
    42
slouken@1662
    43
int
slouken@1662
    44
main (int argc, char *argv[])
slouken@0
    45
{
slouken@1662
    46
    SDL_Thread *thread;
slouken@0
    47
slouken@1662
    48
    /* Load the SDL library */
slouken@1662
    49
    if (SDL_Init (0) < 0) {
slouken@1662
    50
        fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ());
slouken@1662
    51
        return (1);
slouken@1662
    52
    }
slouken@0
    53
slouken@1662
    54
    alive = 1;
slouken@1662
    55
    thread = SDL_CreateThread (ThreadFunc, "#1");
slouken@1662
    56
    if (thread == NULL) {
slouken@1662
    57
        fprintf (stderr, "Couldn't create thread: %s\n", SDL_GetError ());
slouken@1662
    58
        quit (1);
slouken@1662
    59
    }
slouken@1662
    60
    SDL_Delay (5 * 1000);
slouken@1662
    61
    printf ("Waiting for thread #1\n");
slouken@1662
    62
    alive = 0;
slouken@1662
    63
    SDL_WaitThread (thread, NULL);
slouken@0
    64
slouken@1662
    65
    alive = 1;
slouken@1662
    66
    thread = SDL_CreateThread (ThreadFunc, "#2");
slouken@1662
    67
    if (thread == NULL) {
slouken@1662
    68
        fprintf (stderr, "Couldn't create thread: %s\n", SDL_GetError ());
slouken@1662
    69
        quit (1);
slouken@1662
    70
    }
slouken@1662
    71
    SDL_Delay (5 * 1000);
slouken@1662
    72
    printf ("Killing thread #2\n");
slouken@1662
    73
    SDL_KillThread (thread);
slouken@0
    74
slouken@1662
    75
    alive = 1;
slouken@1662
    76
    signal (SIGTERM, killed);
slouken@1662
    77
    thread = SDL_CreateThread (ThreadFunc, "#3");
slouken@1662
    78
    if (thread == NULL) {
slouken@1662
    79
        fprintf (stderr, "Couldn't create thread: %s\n", SDL_GetError ());
slouken@1662
    80
        quit (1);
slouken@1662
    81
    }
slouken@1662
    82
    raise (SIGTERM);
slouken@0
    83
slouken@1662
    84
    SDL_Quit ();                /* Never reached */
slouken@1662
    85
    return (0);                 /* Never reached */
slouken@0
    86
}