-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_pthread.c
111 lines (98 loc) · 3.71 KB
/
test_pthread.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
107
108
109
110
/*
** GNU Pth - The GNU Portable Threads
** Copyright (c) 1999-2006 Ralf S. Engelschall <[email protected]>
**
** This file is part of GNU Pth, a non-preemptive thread scheduling
** library which can be found at http://www.gnu.org/software/pth/.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License as published by the Free Software Foundation; either
** version 2.1 of the License, or (at your option) any later version.
**
** This library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this library; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
** USA, or contact Ralf S. Engelschall <[email protected]>.
**
** test_pthread.c: Pth test program (pthread API)
*/
/* ``You can check out any time you
like, but you can never leave.''
-- The Eagles, Hotel California */
#ifdef GLOBAL
#include <pthread.h>
#else
#include "pthread.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#define die(str) \
do { \
fprintf(stderr, "**die: %s: errno=%d\n", str, errno); \
exit(1); \
} while (0)
static void *child(void *_arg)
{
char *name = (char *)_arg;
int i;
fprintf(stderr, "child: startup %s\n", name);
for (i = 0; i < 100; i++) {
if (i++ % 10 == 0)
sleep(1);
fprintf(stderr, "child: %s counts i=%d\n", name, i);
}
fprintf(stderr, "child: shutdown %s\n", name);
return _arg;
}
int main(int argc, char *argv[])
{
pthread_attr_t thread_attr;
pthread_t thread[4];
char *rc;
fprintf(stderr, "main: init\n");
fprintf(stderr, "main: initializing attribute object\n");
if (pthread_attr_init(&thread_attr) != 0)
die("pthread_attr_init");
if (pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE) != 0)
die("pthread_attr_setdetachstate");
fprintf(stderr, "main: create thread 1\n");
if (pthread_create(&thread[0], &thread_attr, child, (void *)"foo") != 0)
die("pthread_create");
fprintf(stderr, "main: create thread 2\n");
if (pthread_create(&thread[1], &thread_attr, child, (void *)"bar") != 0)
die("pthread_create");
fprintf(stderr, "main: create thread 3\n");
if (pthread_create(&thread[2], &thread_attr, child, (void *)"baz") != 0)
die("pthread_create");
fprintf(stderr, "main: create thread 4\n");
if (pthread_create(&thread[3], &thread_attr, child, (void *)"quux") != 0)
die("pthread_create");
fprintf(stderr, "main: destroying attribute object\n");
if (pthread_attr_destroy(&thread_attr) != 0)
die("pthread_attr_destroy");
sleep(3);
fprintf(stderr, "main: joining...\n");
if (pthread_join(thread[0], (void **)&rc) != 0)
die("pthread_join");
fprintf(stderr, "main: joined thread: %s\n", rc);
if (pthread_join(thread[1], (void **)&rc) != 0)
die("pthread_join");
fprintf(stderr, "main: joined thread: %s\n", rc);
if (pthread_join(thread[2], (void **)&rc) != 0)
die("pthread_join");
fprintf(stderr, "main: joined thread: %s\n", rc);
if (pthread_join(thread[3], (void **)&rc) != 0)
die("pthread_join");
fprintf(stderr, "main: joined thread: %s\n", rc);
fprintf(stderr, "main: exit\n");
return 0;
}