author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Sat, 13 Dec 2008 17:58:00 +0100 | |
changeset 8 | 96d16dfe787a |
parent 0 | af501b0c1716 |
child 9 | c3fecc82ade6 |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* $Id: threads.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $ |
|
3 |
* $Source: /development/c/demos/threads.c,v $ |
|
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 |
} |
47 |