diff options
author | Sam Lantinga <slouken@libsdl.org> | 2013-07-10 02:32:04 -0700 |
---|---|---|
committer | Sam Lantinga <slouken@libsdl.org> | 2013-07-10 02:32:04 -0700 |
commit | 065b134cd0ce11e85063a2270ee8cdef5831c479 (patch) | |
tree | e8a664d360ce7a2797ce50da80fcfe34bd8d2e8d /test/testthread.c | |
parent | f0adf16c2fc7bf1fd799fa06fea4c76a5fc83383 (diff) | |
download | sdl-065b134cd0ce11e85063a2270ee8cdef5831c479.tar.gz |
Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Diffstat (limited to 'test/testthread.c')
-rw-r--r-- | test/testthread.c | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/test/testthread.c b/test/testthread.c index 21509ff1d..b8f18dc06 100644 --- a/test/testthread.c +++ b/test/testthread.c @@ -19,6 +19,7 @@ #include "SDL.h" #include "SDL_thread.h" +static SDL_TLSID tls; static int alive = 0; /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ @@ -32,8 +33,9 @@ quit(int rc) int SDLCALL ThreadFunc(void *data) { - printf("Started thread %s: My thread id is %lu\n", - (char *) data, SDL_ThreadID()); + SDL_TLSSet(tls, "baby thread"); + printf("Started thread %s: My thread id is %lu, thread data = %s\n", + (char *) data, SDL_ThreadID(), (const char *)SDL_TLSGet(tls)); while (alive) { printf("Thread '%s' is alive!\n", (char *) data); SDL_Delay(1 * 1000); @@ -62,6 +64,11 @@ main(int argc, char *argv[]) return (1); } + tls = SDL_TLSCreate(); + SDL_assert(tls); + SDL_TLSSet(tls, "main thread"); + printf("Main thread data initially: %s\n", (const char *)SDL_TLSGet(tls)); + alive = 1; thread = SDL_CreateThread(ThreadFunc, "One", "#1"); if (thread == NULL) { @@ -73,6 +80,8 @@ main(int argc, char *argv[]) alive = 0; SDL_WaitThread(thread, NULL); + printf("Main thread data finally: %s\n", (const char *)SDL_TLSGet(tls)); + alive = 1; signal(SIGTERM, killed); thread = SDL_CreateThread(ThreadFunc, "Two", "#2"); |