source: liacs/os/assignment4/assignment4b.c@ 94

Last change on this file since 94 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
10static int x = 0;
11static int y = 0;
12
13void *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
26void *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
39int 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.