2 ** GNU Pth - The GNU Portable Threads
3 ** Copyright (c) 1999-2006 Ralf S. Engelschall <rse@engelschall.com>
5 ** This file is part of GNU Pth, a non-preemptive thread scheduling
6 ** library which can be found at http://www.gnu.org/software/pth/.
8 ** This library is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU Lesser General Public
10 ** License as published by the Free Software Foundation; either
11 ** version 2.1 of the License, or (at your option) any later version.
13 ** This library is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ** Lesser General Public License for more details.
18 ** You should have received a copy of the GNU Lesser General Public
19 ** License along with this library; if not, write to the Free Software
20 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 ** USA, or contact Ralf S. Engelschall <rse@engelschall.com>.
23 ** test_pthread.c: Pth test program (pthread API)
25 /* ``You can check out any time you
26 like, but you can never leave.''
27 -- The Eagles, Hotel California */
42 fprintf(stderr, "**die: %s: errno=%d\n", str, errno); \
46 static void *child(void *_arg)
48 char *name = (char *)_arg;
51 fprintf(stderr, "child: startup %s\n", name);
52 for (i = 0; i < 100; i++) {
55 fprintf(stderr, "child: %s counts i=%d\n", name, i);
57 fprintf(stderr, "child: shutdown %s\n", name);
61 int main(int argc, char *argv[])
63 pthread_attr_t thread_attr;
67 fprintf(stderr, "main: init\n");
69 fprintf(stderr, "main: initializing attribute object\n");
70 if (pthread_attr_init(&thread_attr) != 0)
71 die("pthread_attr_init");
72 if (pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE) != 0)
73 die("pthread_attr_setdetachstate");
75 fprintf(stderr, "main: create thread 1\n");
76 if (pthread_create(&thread[0], &thread_attr, child, (void *)"foo") != 0)
77 die("pthread_create");
78 fprintf(stderr, "main: create thread 2\n");
79 if (pthread_create(&thread[1], &thread_attr, child, (void *)"bar") != 0)
80 die("pthread_create");
81 fprintf(stderr, "main: create thread 3\n");
82 if (pthread_create(&thread[2], &thread_attr, child, (void *)"baz") != 0)
83 die("pthread_create");
84 fprintf(stderr, "main: create thread 4\n");
85 if (pthread_create(&thread[3], &thread_attr, child, (void *)"quux") != 0)
86 die("pthread_create");
88 fprintf(stderr, "main: destroying attribute object\n");
89 if (pthread_attr_destroy(&thread_attr) != 0)
90 die("pthread_attr_destroy");
94 fprintf(stderr, "main: joining...\n");
95 if (pthread_join(thread[0], (void **)&rc) != 0)
97 fprintf(stderr, "main: joined thread: %s\n", rc);
98 if (pthread_join(thread[1], (void **)&rc) != 0)
100 fprintf(stderr, "main: joined thread: %s\n", rc);
101 if (pthread_join(thread[2], (void **)&rc) != 0)
103 fprintf(stderr, "main: joined thread: %s\n", rc);
104 if (pthread_join(thread[3], (void **)&rc) != 0)
106 fprintf(stderr, "main: joined thread: %s\n", rc);
108 fprintf(stderr, "main: exit\n");