#include /* for EXIT_SUCCESS */ #include /* for fprintf */ #include /* for sleep */ #include "semaphore.h" #define THREADS 10 static semaphore_p sem; void* threadMain(void* arg){ int me = (int)arg; fprintf(stderr, "Thread %d waiting.\n", me); waitSemaphore(sem); fprintf(stderr, "Thread %d woken up from wait.\n", me); pthread_exit(NULL); } int main(int argc, char** argv){ pthread_t threads[THREADS]; int i; /* let's start of with the semaphore locked. */ sem = newSemaphore(0); for(i = 0; i < THREADS; i++) pthread_create(&threads[i], NULL, threadMain, (void *)i); fprintf(stderr, "Main thread finished creating threads\n"); for(i = 0; i < THREADS; i++){ fprintf(stderr, "Main thread posting on iteration %d\n", i); postSemaphore(sem); sleep(1); } freeSemaphore(sem); exit(EXIT_SUCCESS); }