test/testhread.c
author Holmes Futrell <hfutrell@umail.ucsb.edu>
Fri, 18 Jul 2008 19:23:13 +0000
branchgsoc2008_iphone
changeset 2377 e9a0bad0ea07
parent 1895 c121d94672cb
child 2779 4436464c4f51
permissions -rw-r--r--
Just renamed it...
     1 
     2 /* Simple test of the SDL threading code */
     3 
     4 #include <stdio.h>
     5 #include <stdlib.h>
     6 #include <signal.h>
     7 
     8 #include "SDL.h"
     9 #include "SDL_thread.h"
    10 
    11 static int alive = 0;
    12 
    13 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    14 static void
    15 quit(int rc)
    16 {
    17     SDL_Quit();
    18     exit(rc);
    19 }
    20 
    21 int SDLCALL
    22 ThreadFunc(void *data)
    23 {
    24     printf("Started thread %s: My thread id is %u\n",
    25            (char *) data, SDL_ThreadID());
    26     while (alive) {
    27         printf("Thread '%s' is alive!\n", (char *) data);
    28         SDL_Delay(1 * 1000);
    29     }
    30     printf("Thread '%s' exiting!\n", (char *) data);
    31     return (0);
    32 }
    33 
    34 static void
    35 killed(int sig)
    36 {
    37     printf("Killed with SIGTERM, waiting 5 seconds to exit\n");
    38     SDL_Delay(5 * 1000);
    39     alive = 0;
    40     quit(0);
    41 }
    42 
    43 int
    44 main(int argc, char *argv[])
    45 {
    46     SDL_Thread *thread;
    47 
    48     /* Load the SDL library */
    49     if (SDL_Init(0) < 0) {
    50         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
    51         return (1);
    52     }
    53 
    54     alive = 1;
    55     thread = SDL_CreateThread(ThreadFunc, "#1");
    56     if (thread == NULL) {
    57         fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
    58         quit(1);
    59     }
    60     SDL_Delay(5 * 1000);
    61     printf("Waiting for thread #1\n");
    62     alive = 0;
    63     SDL_WaitThread(thread, NULL);
    64 
    65     alive = 1;
    66     thread = SDL_CreateThread(ThreadFunc, "#2");
    67     if (thread == NULL) {
    68         fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
    69         quit(1);
    70     }
    71     SDL_Delay(5 * 1000);
    72     printf("Killing thread #2\n");
    73     SDL_KillThread(thread);
    74 
    75     alive = 1;
    76     signal(SIGTERM, killed);
    77     thread = SDL_CreateThread(ThreadFunc, "#3");
    78     if (thread == NULL) {
    79         fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
    80         quit(1);
    81     }
    82     raise(SIGTERM);
    83 
    84     SDL_Quit();                 /* Never reached */
    85     return (0);                 /* Never reached */
    86 }