test/testerror.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 16 Dec 2009 04:48:11 +0000
changeset 3578 0d1b16ee0bca
parent 1895 c121d94672cb
child 5535 96594ac5fd1a
permissions -rw-r--r--
Fixed bug #741

The thread ID is an unsigned long so it can hold pthread_t so people can do naughty things with it.

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