test/testerror.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 02 Oct 2011 00:29:16 -0400
changeset 5969 3a041d215edc
parent 5535 96594ac5fd1a
child 7517 965d57022c01
permissions -rw-r--r--
1.3 API CHANGE: Add support for naming threads.
     1 /*
     2   Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     3 
     4   This software is provided 'as-is', without any express or implied
     5   warranty.  In no event will the authors be held liable for any damages
     6   arising from the use of this software.
     7 
     8   Permission is granted to anyone to use this software for any purpose,
     9   including commercial applications, and to alter it and redistribute it
    10   freely.
    11 */
    12 
    13 /* Simple test of the SDL threading code and error handling */
    14 
    15 #include <stdio.h>
    16 #include <stdlib.h>
    17 #include <signal.h>
    18 
    19 #include "SDL.h"
    20 #include "SDL_thread.h"
    21 
    22 static int alive = 0;
    23 
    24 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    25 static void
    26 quit(int rc)
    27 {
    28     SDL_Quit();
    29     exit(rc);
    30 }
    31 
    32 int SDLCALL
    33 ThreadFunc(void *data)
    34 {
    35     /* Set the child thread error string */
    36     SDL_SetError("Thread %s (%lu) had a problem: %s",
    37                  (char *) data, SDL_ThreadID(), "nevermind");
    38     while (alive) {
    39         printf("Thread '%s' is alive!\n", (char *) data);
    40         SDL_Delay(1 * 1000);
    41     }
    42     printf("Child thread error string: %s\n", SDL_GetError());
    43     return (0);
    44 }
    45 
    46 int
    47 main(int argc, char *argv[])
    48 {
    49     SDL_Thread *thread;
    50 
    51     /* Load the SDL library */
    52     if (SDL_Init(0) < 0) {
    53         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
    54         return (1);
    55     }
    56 
    57     /* Set the error value for the main thread */
    58     SDL_SetError("No worries");
    59 
    60     alive = 1;
    61     thread = SDL_CreateThread(ThreadFunc, NULL, "#1");
    62     if (thread == NULL) {
    63         fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
    64         quit(1);
    65     }
    66     SDL_Delay(5 * 1000);
    67     printf("Waiting for thread #1\n");
    68     alive = 0;
    69     SDL_WaitThread(thread, NULL);
    70 
    71     printf("Main thread error string: %s\n", SDL_GetError());
    72 
    73     SDL_Quit();
    74     return (0);
    75 }