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

Latest commit

 

History

History
86 lines (73 loc) · 1.87 KB

testhread.c

File metadata and controls

86 lines (73 loc) · 1.87 KB
 
Apr 26, 2001
Apr 26, 2001
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;
Sep 28, 2005
Sep 28, 2005
13
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
May 28, 2006
May 28, 2006
14
static void
May 29, 2006
May 29, 2006
15
quit(int rc)
Sep 28, 2005
Sep 28, 2005
16
{
May 29, 2006
May 29, 2006
17
18
SDL_Quit();
exit(rc);
Sep 28, 2005
Sep 28, 2005
19
20
}
May 28, 2006
May 28, 2006
21
int SDLCALL
May 29, 2006
May 29, 2006
22
ThreadFunc(void *data)
Apr 26, 2001
Apr 26, 2001
23
{
May 29, 2006
May 29, 2006
24
25
printf("Started thread %s: My thread id is %u\n",
(char *) data, SDL_ThreadID());
May 28, 2006
May 28, 2006
26
while (alive) {
May 29, 2006
May 29, 2006
27
28
printf("Thread '%s' is alive!\n", (char *) data);
SDL_Delay(1 * 1000);
May 28, 2006
May 28, 2006
29
}
May 29, 2006
May 29, 2006
30
printf("Thread '%s' exiting!\n", (char *) data);
May 28, 2006
May 28, 2006
31
return (0);
Apr 26, 2001
Apr 26, 2001
32
33
}
May 28, 2006
May 28, 2006
34
static void
May 29, 2006
May 29, 2006
35
killed(int sig)
Apr 26, 2001
Apr 26, 2001
36
{
May 29, 2006
May 29, 2006
37
38
printf("Killed with SIGTERM, waiting 5 seconds to exit\n");
SDL_Delay(5 * 1000);
May 28, 2006
May 28, 2006
39
alive = 0;
May 29, 2006
May 29, 2006
40
quit(0);
Apr 26, 2001
Apr 26, 2001
41
42
}
May 28, 2006
May 28, 2006
43
int
May 29, 2006
May 29, 2006
44
main(int argc, char *argv[])
Apr 26, 2001
Apr 26, 2001
45
{
May 28, 2006
May 28, 2006
46
SDL_Thread *thread;
Apr 26, 2001
Apr 26, 2001
47
May 28, 2006
May 28, 2006
48
/* Load the SDL library */
May 29, 2006
May 29, 2006
49
50
if (SDL_Init(0) < 0) {
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
May 28, 2006
May 28, 2006
51
52
return (1);
}
Apr 26, 2001
Apr 26, 2001
53
May 28, 2006
May 28, 2006
54
alive = 1;
May 29, 2006
May 29, 2006
55
thread = SDL_CreateThread(ThreadFunc, "#1");
May 28, 2006
May 28, 2006
56
if (thread == NULL) {
May 29, 2006
May 29, 2006
57
58
fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
quit(1);
May 28, 2006
May 28, 2006
59
}
May 29, 2006
May 29, 2006
60
61
SDL_Delay(5 * 1000);
printf("Waiting for thread #1\n");
May 28, 2006
May 28, 2006
62
alive = 0;
May 29, 2006
May 29, 2006
63
SDL_WaitThread(thread, NULL);
Apr 26, 2001
Apr 26, 2001
64
May 28, 2006
May 28, 2006
65
alive = 1;
May 29, 2006
May 29, 2006
66
thread = SDL_CreateThread(ThreadFunc, "#2");
May 28, 2006
May 28, 2006
67
if (thread == NULL) {
May 29, 2006
May 29, 2006
68
69
fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
quit(1);
May 28, 2006
May 28, 2006
70
}
May 29, 2006
May 29, 2006
71
72
73
SDL_Delay(5 * 1000);
printf("Killing thread #2\n");
SDL_KillThread(thread);
Apr 26, 2001
Apr 26, 2001
74
May 28, 2006
May 28, 2006
75
alive = 1;
May 29, 2006
May 29, 2006
76
77
signal(SIGTERM, killed);
thread = SDL_CreateThread(ThreadFunc, "#3");
May 28, 2006
May 28, 2006
78
if (thread == NULL) {
May 29, 2006
May 29, 2006
79
80
fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
quit(1);
May 28, 2006
May 28, 2006
81
}
May 29, 2006
May 29, 2006
82
raise(SIGTERM);
Apr 26, 2001
Apr 26, 2001
83
May 29, 2006
May 29, 2006
84
SDL_Quit(); /* Never reached */
May 28, 2006
May 28, 2006
85
return (0); /* Never reached */
Apr 26, 2001
Apr 26, 2001
86
}