test/testthread.c
changeset 5508 60b63c32f06d
parent 3578 0d1b16ee0bca
child 5535 96594ac5fd1a
equal deleted inserted replaced
5507:6275a16e63c2 5508:60b63c32f06d
       
     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 %lu\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     signal(SIGTERM, killed);
       
    67     thread = SDL_CreateThread(ThreadFunc, "#2");
       
    68     if (thread == NULL) {
       
    69         fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
       
    70         quit(1);
       
    71     }
       
    72     raise(SIGTERM);
       
    73 
       
    74     SDL_Quit();                 /* Never reached */
       
    75     return (0);                 /* Never reached */
       
    76 }