/************************************************************ File: bridge.h Description: A program that simulates a one-lane bridge and some cars trying to get through it. Declaration of the bridge monitor. Author: Dana Vrajitoru Organization: IUSB Date: December 7, 2004 **************************************************************/ #ifndef BRIDGE_H #define BRIDGE_H #include // A monitor class for a single-lane bridge that can only be crossed // in one direction. In this implementation, every car (thread) // requesting to cross the bridge waits on a conditional // variable. when the last car crosses the bridge in one direction, it // allows all the cars waiting at that moment to cross in the opposite // direction to cross the bridge. This is not necessarily a fair // implementation. class Bridge_monitor { private: pthread_mutex_t mutex; pthread_cond_t cond_left, cond_right; int crossing, waiting_left, waiting_right; public: // Simple init function setting all the values of the variables to // 0. void init(); // A tread is calling these when it wants to cross the bridge to the // left or right respectively. If the bridge is not free, the thread // should be waiting on the appropriate conditional variable to be // allowed to cross the bridge. void request_left(); void request_right(); // A thread is calling these after it has finished crossing the // bridge. The last thread to cross should broadcast the conditional // variable in the opposite direction if any thread is waiting on // it, or the one in the same direction if not. void release_left(); void release_right(); }; #endif