author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Sat, 13 Dec 2008 17:58:14 +0100 | |
changeset 21 | 403742321c65 |
parent 9 | c3fecc82ade6 |
child 27 | 81a574d60c15 |
permissions | -rw-r--r-- |
0 | 1 |
/** |
9
c3fecc82ade6
standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
8
diff
changeset
|
2 |
* test/demos/threads.c |
c3fecc82ade6
standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
8
diff
changeset
|
3 |
* Copyright (C) 2008 Markus Broeker |
0 | 4 |
*/ |
5 |
||
6 |
#include <stdio.h> |
|
7 |
#include <stdlib.h> |
|
8 |
#include <pthread.h> |
|
9 |
#include <time.h> |
|
10 |
||
11 |
void do_some_work (void) |
|
12 |
{ |
|
13 |
time_t start_time = time (NULL); |
|
14 |
||
15 |
while (time (NULL) == start_time); /* busy-wait for 0-1 seconds */ |
|
16 |
} |
|
17 |
||
8
96d16dfe787a
We use return EXIT_SUCCESS instead of return 0
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
18 |
void *thread_func (void *vptr_args) |
96d16dfe787a
We use return EXIT_SUCCESS instead of return 0
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
19 |
{ |
96d16dfe787a
We use return EXIT_SUCCESS instead of return 0
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
20 |
int i; |
96d16dfe787a
We use return EXIT_SUCCESS instead of return 0
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
21 |
|
96d16dfe787a
We use return EXIT_SUCCESS instead of return 0
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
22 |
for (i = 0; i < 20; ++i) { |
96d16dfe787a
We use return EXIT_SUCCESS instead of return 0
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
23 |
fprintf (stderr, " b\n"); |
96d16dfe787a
We use return EXIT_SUCCESS instead of return 0
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
24 |
|
96d16dfe787a
We use return EXIT_SUCCESS instead of return 0
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
25 |
do_some_work (); |
96d16dfe787a
We use return EXIT_SUCCESS instead of return 0
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
26 |
} |
96d16dfe787a
We use return EXIT_SUCCESS instead of return 0
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
27 |
return NULL; |
96d16dfe787a
We use return EXIT_SUCCESS instead of return 0
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
28 |
} |
96d16dfe787a
We use return EXIT_SUCCESS instead of return 0
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
29 |
|
0 | 30 |
int main (int argc, char **argv) |
31 |
{ |
|
32 |
int i; |
|
33 |
pthread_t thread; |
|
34 |
||
35 |
pthread_create (&thread, NULL, &thread_func, NULL); |
|
36 |
||
37 |
for (i = 0; i < 20; ++i) { |
|
38 |
fprintf (stdout, "a\n"); |
|
39 |
||
40 |
do_some_work (); |
|
41 |
} |
|
42 |
||
43 |
pthread_join (thread, NULL); |
|
44 |
||
8
96d16dfe787a
We use return EXIT_SUCCESS instead of return 0
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
45 |
return EXIT_SUCCESS; |
0 | 46 |
} |