Last change
on this file since 172 was 2, checked in by Rick van der Zwet, 15 years ago |
Initial import of data of old repository ('data') worth keeping (e.g. tracking
means of URL access statistics)
|
File size:
906 bytes
|
Line | |
---|
1 |
|
---|
2 |
|
---|
3 | #include <sysexits.h>
|
---|
4 | #include <stdlib.h>
|
---|
5 | #include <pthread.h>
|
---|
6 | #include <stdio.h>
|
---|
7 | #include <unistd.h>
|
---|
8 |
|
---|
9 |
|
---|
10 | static int x = 0;
|
---|
11 | static int y = 0;
|
---|
12 |
|
---|
13 | void *procA() {
|
---|
14 | int local_y = 0;
|
---|
15 |
|
---|
16 | //write to x, then wait till y got updated
|
---|
17 | for ( ;; ) {
|
---|
18 | x++;
|
---|
19 | while ( y == local_y ) { usleep(1); }
|
---|
20 | local_y = y;
|
---|
21 |
|
---|
22 | }
|
---|
23 | pthread_exit(NULL);
|
---|
24 | }
|
---|
25 |
|
---|
26 | void *procB() {
|
---|
27 | int local_x = 0;
|
---|
28 |
|
---|
29 | //wait till x got updated, then write to y
|
---|
30 | for ( ;; ) {
|
---|
31 | while ( x == local_x ) { usleep(1); }
|
---|
32 | local_x = x;
|
---|
33 | y++;
|
---|
34 | }
|
---|
35 | pthread_exit(NULL);
|
---|
36 | }
|
---|
37 |
|
---|
38 |
|
---|
39 | int main (int argc, char * argv[]) {
|
---|
40 |
|
---|
41 | //create 2 threads
|
---|
42 | pthread_t thread0, thread1;
|
---|
43 | pthread_create(&thread0, NULL, procA, NULL);
|
---|
44 | pthread_create(&thread1, NULL, procB, NULL);
|
---|
45 |
|
---|
46 | //debugging
|
---|
47 | for ( ;; ) {
|
---|
48 | printf("x:%i y:%i\n", x, y);
|
---|
49 | sleep(1);
|
---|
50 | }
|
---|
51 | pthread_exit(NULL);
|
---|
52 | return(EX_OK);
|
---|
53 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.