I recently read the exclusive queue (exclusive ballroom dancers) problem on the Little Book of Semaphores by Allen Downey.His solution included a single mutex for both the leaders and the followers. Could there be anything wrong with using two separate mutexes for leaders and followers? After all, a check is done at the end of the dance part. Below is my solution.Initialization:semaphore leaderMutex = 1;semaphore followerMutex = 1;semaphore leaderHasFinished=0;semaphore followerHasFinished=0;int numLeaders=0;int numFollowers=0;Leader:leaderMutex.wait();numLeaders++;while(numFollowers<=0);dance();numLeaders--;leaderHasFinished.signal();followerHasFinished.wait();leaderMutex.signal();Follower:followerMutex.wait();numFollowers++;while(numLeaders<=0);dance();numFollowers--;followerHasFinished.signal();leaderHasFinished.wait();followerMutex.signal();
Using two separate mutexes for the Exclusive Queue Problem (Little Book of Semaphores)
concurrency;synchronization