This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
testhread.c
86 lines (73 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
/* Simple test of the SDL threading code */
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include "SDL.h"
#include "SDL_thread.h"
static int alive = 0;
13
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
14
static void
15
quit(int rc)
16
{
17
18
SDL_Quit();
exit(rc);
19
20
}
21
int SDLCALL
22
ThreadFunc(void *data)
23
{
24
25
printf("Started thread %s: My thread id is %u\n",
(char *) data, SDL_ThreadID());
26
while (alive) {
27
28
printf("Thread '%s' is alive!\n", (char *) data);
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
38
printf("Killed with SIGTERM, waiting 5 seconds to exit\n");
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
50
if (SDL_Init(0) < 0) {
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
51
52
return (1);
}
53
54
alive = 1;
55
thread = SDL_CreateThread(ThreadFunc, "#1");
56
if (thread == NULL) {
57
58
fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
quit(1);
59
}
60
61
SDL_Delay(5 * 1000);
printf("Waiting for thread #1\n");
62
alive = 0;
63
SDL_WaitThread(thread, NULL);
64
65
alive = 1;
66
thread = SDL_CreateThread(ThreadFunc, "#2");
67
if (thread == NULL) {
68
69
fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
quit(1);
70
}
71
72
73
SDL_Delay(5 * 1000);
printf("Killing thread #2\n");
SDL_KillThread(thread);
74
75
alive = 1;
76
77
signal(SIGTERM, killed);
thread = SDL_CreateThread(ThreadFunc, "#3");
78
if (thread == NULL) {
79
80
fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
quit(1);
81
}
82
raise(SIGTERM);
83
84
SDL_Quit(); /* Never reached */
85
return (0); /* Never reached */
86
}