test/testthread.c
changeset 7391 a29895dc5e9a
parent 5969 3a041d215edc
child 7393 358696c354a8
equal deleted inserted replaced
7390:e4b98404baa4 7391:a29895dc5e9a
    17 #include <signal.h>
    17 #include <signal.h>
    18 
    18 
    19 #include "SDL.h"
    19 #include "SDL.h"
    20 #include "SDL_thread.h"
    20 #include "SDL_thread.h"
    21 
    21 
       
    22 static SDL_TLSID tls;
    22 static int alive = 0;
    23 static int alive = 0;
    23 
    24 
    24 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    25 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    25 static void
    26 static void
    26 quit(int rc)
    27 quit(int rc)
    30 }
    31 }
    31 
    32 
    32 int SDLCALL
    33 int SDLCALL
    33 ThreadFunc(void *data)
    34 ThreadFunc(void *data)
    34 {
    35 {
    35     printf("Started thread %s: My thread id is %lu\n",
    36     SDL_TLSSet(tls, "baby thread");
    36            (char *) data, SDL_ThreadID());
    37     printf("Started thread %s: My thread id is %lu, thread data = %s\n",
       
    38            (char *) data, SDL_ThreadID(), (const char *)SDL_TLSGet(tls));
    37     while (alive) {
    39     while (alive) {
    38         printf("Thread '%s' is alive!\n", (char *) data);
    40         printf("Thread '%s' is alive!\n", (char *) data);
    39         SDL_Delay(1 * 1000);
    41         SDL_Delay(1 * 1000);
    40     }
    42     }
    41     printf("Thread '%s' exiting!\n", (char *) data);
    43     printf("Thread '%s' exiting!\n", (char *) data);
    60     if (SDL_Init(0) < 0) {
    62     if (SDL_Init(0) < 0) {
    61         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
    63         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
    62         return (1);
    64         return (1);
    63     }
    65     }
    64 
    66 
       
    67     tls = SDL_TLSCreate();
       
    68     SDL_assert(tls);
       
    69     SDL_TLSSet(tls, "main thread");
       
    70     printf("Main thread data initially: %s\n", (const char *)SDL_TLSGet(tls));
       
    71 
    65     alive = 1;
    72     alive = 1;
    66     thread = SDL_CreateThread(ThreadFunc, "One", "#1");
    73     thread = SDL_CreateThread(ThreadFunc, "One", "#1");
    67     if (thread == NULL) {
    74     if (thread == NULL) {
    68         fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
    75         fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
    69         quit(1);
    76         quit(1);
    70     }
    77     }
    71     SDL_Delay(5 * 1000);
    78     SDL_Delay(5 * 1000);
    72     printf("Waiting for thread #1\n");
    79     printf("Waiting for thread #1\n");
    73     alive = 0;
    80     alive = 0;
    74     SDL_WaitThread(thread, NULL);
    81     SDL_WaitThread(thread, NULL);
       
    82 
       
    83     printf("Main thread data finally: %s\n", (const char *)SDL_TLSGet(tls));
    75 
    84 
    76     alive = 1;
    85     alive = 1;
    77     signal(SIGTERM, killed);
    86     signal(SIGTERM, killed);
    78     thread = SDL_CreateThread(ThreadFunc, "Two", "#2");
    87     thread = SDL_CreateThread(ThreadFunc, "Two", "#2");
    79     if (thread == NULL) {
    88     if (thread == NULL) {