test/testerror.c
author David Ludwig <dludwig@pobox.com>
Tue, 12 Feb 2013 12:57:06 -0500
changeset 8424 3cf9501008f1
parent 5969 3a041d215edc
child 7517 965d57022c01
permissions -rw-r--r--
WinRT: fixed bug: SDL_CreateWindow wouldn't work after an initial window was created + destroyed
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 and error handling */
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@1895
    35
    /* Set the child thread error string */
slouken@3578
    36
    SDL_SetError("Thread %s (%lu) had a problem: %s",
slouken@1895
    37
                 (char *) data, SDL_ThreadID(), "nevermind");
slouken@1895
    38
    while (alive) {
slouken@1895
    39
        printf("Thread '%s' is alive!\n", (char *) data);
slouken@1895
    40
        SDL_Delay(1 * 1000);
slouken@1895
    41
    }
slouken@1895
    42
    printf("Child thread error string: %s\n", SDL_GetError());
slouken@1895
    43
    return (0);
slouken@0
    44
}
slouken@0
    45
slouken@1895
    46
int
slouken@1895
    47
main(int argc, char *argv[])
slouken@0
    48
{
slouken@1895
    49
    SDL_Thread *thread;
slouken@0
    50
slouken@1895
    51
    /* Load the SDL library */
slouken@1895
    52
    if (SDL_Init(0) < 0) {
slouken@1895
    53
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@1895
    54
        return (1);
slouken@1895
    55
    }
slouken@0
    56
slouken@1895
    57
    /* Set the error value for the main thread */
slouken@1895
    58
    SDL_SetError("No worries");
slouken@0
    59
slouken@1895
    60
    alive = 1;
icculus@5969
    61
    thread = SDL_CreateThread(ThreadFunc, NULL, "#1");
slouken@1895
    62
    if (thread == NULL) {
slouken@1895
    63
        fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
slouken@1895
    64
        quit(1);
slouken@1895
    65
    }
slouken@1895
    66
    SDL_Delay(5 * 1000);
slouken@1895
    67
    printf("Waiting for thread #1\n");
slouken@1895
    68
    alive = 0;
slouken@1895
    69
    SDL_WaitThread(thread, NULL);
slouken@0
    70
slouken@1895
    71
    printf("Main thread error string: %s\n", SDL_GetError());
slouken@0
    72
slouken@1895
    73
    SDL_Quit();
slouken@1895
    74
    return (0);
slouken@0
    75
}