#include #include #include #include #include static int x = 0; static int y = 0; void *procA() { int local_y = 0; //write to x, then wait till y got updated for ( ;; ) { x++; while ( y == local_y ) { usleep(1); } local_y = y; } pthread_exit(NULL); } void *procB() { int local_x = 0; //wait till x got updated, then write to y for ( ;; ) { while ( x == local_x ) { usleep(1); } local_x = x; y++; } pthread_exit(NULL); } int main (int argc, char * argv[]) { //create 2 threads pthread_t thread0, thread1; pthread_create(&thread0, NULL, procA, NULL); pthread_create(&thread1, NULL, procB, NULL); //debugging for ( ;; ) { printf("x:%i y:%i\n", x, y); sleep(1); } pthread_exit(NULL); return(EX_OK); }