// Just counting again, but with protected access to the counter. // N is smaller than for "badcounter", because it gets too slow // with the N == 2000000 (and there is nothing interesting to see - it works) // // // Compile: gcc goodcounter.c -o goodcounter -lpthread // Run: ./goodcounter #include #include #include #include #include #define P 4 #define N 200000 // make sure that the compiler doesn't // optimise this into a register volatile int target; sem_t lock; void *adderthread (void *arg) { int i; for (i=0; i