#ifndef SEMAPHORE_H_ #define SEMAPHORE_H_ #include // has pthread_ routines #include // has sem_ routines // ---- Class for semaphore ------------------------------------------ // By defining a class for semaphores, we ensure that sem_init // and sem_destroy are called automagically (respectively, // when a semaphoreObj is declared and goes out of scope). class semaphoreObj { public: // Constructor (one parameter) and destructor. semaphoreObj(const unsigned int initVal) { sem_init(&theSemaphore, 0, initVal); } ~semaphoreObj(void) { sem_destroy(&theSemaphore); } private: // Copy constructor and assignment operator: Make private so // they can't be used (and we don't have to write them). semaphoreObj(const semaphoreObj & d); semaphoreObj & operator= (const semaphoreObj & d); public: // Function to execute "wait" on semaphore. void wait(void) { sem_wait(&theSemaphore); } // Function to execute "signal" on semaphore. // (Called post to be consistent with sem_ routine naming. void post(void) { sem_post(&theSemaphore); } private: // member variables sem_t theSemaphore; }; #endif // SEMAPHORE_H_