/* threadsem.c - Same was threadmutex.c, but now using posix semaphores, not mutexes. compile with gcc -Wall threadmutex.c -o threadmutex -lpthread */ #include #include #include #include #include #include #define THREADSCOUNT 3 static pthread_t ts[THREADSCOUNT]; static sem_t semaphore; static struct { int x; int y;} foo; /*This is a global data structure shared by threads*/ static void moo(int * a); int main(void) { int i; int * who; /* Create a mutex */ if (sem_init(&semaphore, 0, 1)) { perror("sem_init"); exit(1);} /* Create threads */ for (i=0; i