2 Copyright (C) 1997-2015 Sam Lantinga <slouken@libsdl.org>
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.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
13 /* Simple test of the SDL threading code */
24 static char volatile time_for_threads_to_die[NUMTHREADS];
26 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
35 SubThreadFunc(void *data)
37 while (!*(int volatile *) data) {
38 ; /* SDL_Delay(10); *//* do nothing */
44 ThreadFunc(void *data)
46 SDL_Thread *sub_threads[NUMTHREADS];
47 int flags[NUMTHREADS];
49 int tid = (int) (uintptr_t) data;
51 SDL_Log("Creating Thread %d\n", tid);
53 for (i = 0; i < NUMTHREADS; i++) {
55 SDL_snprintf(name, sizeof (name), "Child%d_%d", tid, i);
57 sub_threads[i] = SDL_CreateThread(SubThreadFunc, name, &flags[i]);
60 SDL_Log("Thread '%d' waiting for signal\n", tid);
61 while (time_for_threads_to_die[tid] != 1) {
65 SDL_Log("Thread '%d' sending signals to subthreads\n", tid);
66 for (i = 0; i < NUMTHREADS; i++) {
68 SDL_WaitThread(sub_threads[i], NULL);
71 SDL_Log("Thread '%d' exiting!\n", tid);
77 main(int argc, char *argv[])
79 SDL_Thread *threads[NUMTHREADS];
82 /* Enable standard application logging */
83 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
85 /* Load the SDL library */
86 if (SDL_Init(0) < 0) {
87 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
91 signal(SIGSEGV, SIG_DFL);
92 for (i = 0; i < NUMTHREADS; i++) {
94 SDL_snprintf(name, sizeof (name), "Parent%d", i);
95 time_for_threads_to_die[i] = 0;
96 threads[i] = SDL_CreateThread(ThreadFunc, name, (void*) (uintptr_t) i);
98 if (threads[i] == NULL) {
99 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError());
104 for (i = 0; i < NUMTHREADS; i++) {
105 time_for_threads_to_die[i] = 1;
108 for (i = 0; i < NUMTHREADS; i++) {
109 SDL_WaitThread(threads[i], NULL);