/*! Simple semaphore example with sem_wait/sem_post interface */ #include #include #include #include #define THREADS 6 static sem_t sem; static int work_in_progress = 1; static void *worker(void *p) { long id = (long) p; struct timespec t = {id, 0}; printf("Thread %ld starting\n", id); while (work_in_progress) { sem_wait(&sem); printf("Thread %ld inside C.S.\n", id); nanosleep(&t, NULL); printf("Thread %ld leaving C.S.\n", id); sem_post(&sem); nanosleep(&t, NULL); } printf("Thread %ld exiting\n", id); return NULL; } int main() { long i; pthread_t thr[THREADS]; struct timespec t = { 50, 0 }; sem_init(&sem, 0, 1); for (i = 0; i < THREADS; i++) pthread_create(&thr[i], NULL, worker, (void *) i+1); nanosleep(&t, NULL); work_in_progress = 0; for (i = 0; i < THREADS; i++) pthread_join(thr[i], NULL); return 0; } /* Example run: $ gcc sem_wait.c -pthread -Wall $ ./a.out Thread 6 starting Thread 6 inside C.S. Thread 5 starting Thread 4 starting Thread 3 starting Thread 2 starting Thread 1 starting Thread 6 leaving C.S. Thread 5 inside C.S. ... */