test/testerror.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 and error handling */
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
    /* Set the child thread error string */
slouken@1662
    25
    SDL_SetError ("Thread %s (%d) had a problem: %s",
slouken@1662
    26
                  (char *) data, SDL_ThreadID (), "nevermind");
slouken@1662
    27
    while (alive) {
slouken@1662
    28
        printf ("Thread '%s' is alive!\n", (char *) data);
slouken@1662
    29
        SDL_Delay (1 * 1000);
slouken@1662
    30
    }
slouken@1662
    31
    printf ("Child thread error string: %s\n", SDL_GetError ());
slouken@1662
    32
    return (0);
slouken@0
    33
}
slouken@0
    34
slouken@1662
    35
int
slouken@1662
    36
main (int argc, char *argv[])
slouken@0
    37
{
slouken@1662
    38
    SDL_Thread *thread;
slouken@0
    39
slouken@1662
    40
    /* Load the SDL library */
slouken@1662
    41
    if (SDL_Init (0) < 0) {
slouken@1662
    42
        fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ());
slouken@1662
    43
        return (1);
slouken@1662
    44
    }
slouken@0
    45
slouken@1662
    46
    /* Set the error value for the main thread */
slouken@1662
    47
    SDL_SetError ("No worries");
slouken@0
    48
slouken@1662
    49
    alive = 1;
slouken@1662
    50
    thread = SDL_CreateThread (ThreadFunc, "#1");
slouken@1662
    51
    if (thread == NULL) {
slouken@1662
    52
        fprintf (stderr, "Couldn't create thread: %s\n", SDL_GetError ());
slouken@1662
    53
        quit (1);
slouken@1662
    54
    }
slouken@1662
    55
    SDL_Delay (5 * 1000);
slouken@1662
    56
    printf ("Waiting for thread #1\n");
slouken@1662
    57
    alive = 0;
slouken@1662
    58
    SDL_WaitThread (thread, NULL);
slouken@0
    59
slouken@1662
    60
    printf ("Main thread error string: %s\n", SDL_GetError ());
slouken@0
    61
slouken@1662
    62
    SDL_Quit ();
slouken@1662
    63
    return (0);
slouken@0
    64
}