Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
107 lines (85 loc) · 2.43 KB

torturethread.c

File metadata and controls

107 lines (85 loc) · 2.43 KB
 
Apr 8, 2011
Apr 8, 2011
1
2
3
4
5
6
7
8
9
10
11
/*
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely.
*/
Apr 26, 2001
Apr 26, 2001
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/* Simple test of the SDL threading code */
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include "SDL.h"
#include "SDL_thread.h"
#define NUMTHREADS 10
static char volatile time_for_threads_to_die[NUMTHREADS];
Sep 28, 2005
Sep 28, 2005
27
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
Jul 10, 2006
Jul 10, 2006
28
29
static void
quit(int rc)
Sep 28, 2005
Sep 28, 2005
30
{
Jul 10, 2006
Jul 10, 2006
31
32
SDL_Quit();
exit(rc);
Sep 28, 2005
Sep 28, 2005
33
34
}
Jul 10, 2006
Jul 10, 2006
35
36
37
38
39
40
41
int SDLCALL
SubThreadFunc(void *data)
{
while (!*(int volatile *) data) {
; /*SDL_Delay(10); *//* do nothing */
}
return 0;
Apr 26, 2001
Apr 26, 2001
42
43
}
Jul 10, 2006
Jul 10, 2006
44
45
46
47
48
49
50
int SDLCALL
ThreadFunc(void *data)
{
SDL_Thread *sub_threads[NUMTHREADS];
int flags[NUMTHREADS];
int i;
int tid = (int) (uintptr_t) data;
Apr 26, 2001
Apr 26, 2001
51
Jul 10, 2006
Jul 10, 2006
52
fprintf(stderr, "Creating Thread %d\n", tid);
Apr 26, 2001
Apr 26, 2001
53
Jul 10, 2006
Jul 10, 2006
54
55
56
57
for (i = 0; i < NUMTHREADS; i++) {
flags[i] = 0;
sub_threads[i] = SDL_CreateThread(SubThreadFunc, &flags[i]);
}
Apr 26, 2001
Apr 26, 2001
58
Jul 10, 2006
Jul 10, 2006
59
60
61
62
printf("Thread '%d' waiting for signal\n", tid);
while (time_for_threads_to_die[tid] != 1) {
; /* do nothing */
}
Apr 26, 2001
Apr 26, 2001
63
Jul 10, 2006
Jul 10, 2006
64
65
66
67
68
printf("Thread '%d' sending signals to subthreads\n", tid);
for (i = 0; i < NUMTHREADS; i++) {
flags[i] = 1;
SDL_WaitThread(sub_threads[i], NULL);
}
Apr 26, 2001
Apr 26, 2001
69
Jul 10, 2006
Jul 10, 2006
70
printf("Thread '%d' exiting!\n", tid);
Apr 26, 2001
Apr 26, 2001
71
Jul 10, 2006
Jul 10, 2006
72
return 0;
Apr 26, 2001
Apr 26, 2001
73
74
}
Jul 10, 2006
Jul 10, 2006
75
76
int
main(int argc, char *argv[])
Apr 26, 2001
Apr 26, 2001
77
{
Jul 10, 2006
Jul 10, 2006
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
SDL_Thread *threads[NUMTHREADS];
int i;
/* Load the SDL library */
if (SDL_Init(0) < 0) {
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
return (1);
}
signal(SIGSEGV, SIG_DFL);
for (i = 0; i < NUMTHREADS; i++) {
time_for_threads_to_die[i] = 0;
threads[i] = SDL_CreateThread(ThreadFunc, (void *) (uintptr_t) i);
if (threads[i] == NULL) {
fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
quit(1);
}
}
for (i = 0; i < NUMTHREADS; i++) {
time_for_threads_to_die[i] = 1;
}
for (i = 0; i < NUMTHREADS; i++) {
SDL_WaitThread(threads[i], NULL);
}
SDL_Quit();
return (0);
Apr 26, 2001
Apr 26, 2001
107
}