author | Markus Brökers <mbroeker@largo.homelinux.org> |
Mon, 30 Aug 2010 15:21:47 +0200 | |
changeset 146 | 0ffa20c26b2d |
parent 77 | 49e0babccb23 |
permissions | -rw-r--r-- |
0 | 1 |
/** |
77 | 2 |
* threads.c |
9
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; |
|
27
81a574d60c15
typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
33 |
|
0 | 34 |
pthread_t thread; |
35 |
||
36 |
pthread_create (&thread, NULL, &thread_func, NULL); |
|
37 |
||
38 |
for (i = 0; i < 20; ++i) { |
|
39 |
fprintf (stdout, "a\n"); |
|
40 |
||
41 |
do_some_work (); |
|
42 |
} |
|
43 |
||
44 |
pthread_join (thread, NULL); |
|
45 |
||
8
96d16dfe787a
We use return EXIT_SUCCESS instead of return 0
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
46 |
return EXIT_SUCCESS; |
0 | 47 |
} |