Question
Broken Sleeping Barber Code - C++ We've been given a version of the sleeping barber problem but the code doesn't work, the task is to
Broken Sleeping Barber Code - C++
We've been given a version of the sleeping barber problem but the code doesn't work, the task is to make changes so that the bottom cout will print. We can only make changes under the portion below the part that says "ONLY MAKE CHANGES BELOW THIS LINE" the rest is included for context.
/***************************************************
General Includes
****************************************************/ #include
/***************************************************
Namespaces
****************************************************/ using namespace std;
/***************************************************
Type Definitions
****************************************************/ /* Semaphores */ class binary_semaphore { public: explicit binary_semaphore(int init_count = count_max) : count_(init_count) {}
// P-operation / acquire void wait() { std::unique_lock
// Lockable requirements void lock() { wait(); } bool try_lock() { return try_wait(); } void unlock() { signal(); }
private: static const int count_max = 1; int count_; std::mutex m_; std::condition_variable cv_; };
class general_semaphore { private: std::mutex mutex_; std::condition_variable condition_; unsigned long count_ = 0; // Initialized as locked.
public: void signal() { std::lock_guard
void wait() { std::unique_lock
bool try_wait() { std::lock_guard
/***************************************************
Environment Constants
****************************************************/ /* Environment These can change depending on how big/popular you want your barbershop to be. */ const int NUM_BARBERS = 2; const int NUM_CUSTOMERS = 4;
/***************************************************
Global Variables
****************************************************/ /* Environment These can change depending on how big/popular you want your barbershop to be. */ int numberOfFreeWaitingRoomSeats = 2;
/* Semaphores */ binary_semaphore AccessToWaitingRoomSeats; // semaphore for if you can put people in seats or take them out of seats: auto-assigned to 1 in constructor general_semaphore BarberReady, CustomerReady; // semaphore for if the barber can cut someoneone's hair and if there is a customer who's hair needs to be cut // auto assigned to 0 in constructor
void GetHairCut(int id) { string s = "The Customer " + to_string(id) + " hears Snip Snip "; cout << s; }
void CutHair(int id) { string s = "The Barber " + to_string(id) + " went Snip Snip "; cout << s; }
/***************************************************
ONLY MAKE CHANGES BELOW THIS LINE
****************************************************/
void Barber(int thread_num) { while (true) { CustomerReady.wait(); AccessToWaitingRoomSeats.wait(); numberOfFreeWaitingRoomSeats += 1; BarberReady.signal(); AccessToWaitingRoomSeats.signal(); CutHair(thread_num); } }
void Customer(int thread_num) { AccessToWaitingRoomSeats.wait(); if (numberOfFreeWaitingRoomSeats > 0) { numberOfFreeWaitingRoomSeats -= 1; CustomerReady.signal(); AccessToWaitingRoomSeats.signal(); BarberReady.wait(); GetHairCut(thread_num); } else { //no space, must leave! AccessToWaitingRoomSeats.signal(); } }
int main() { int threadnum = NUM_BARBERS + NUM_CUSTOMERS; thread threads[NUM_BARBERS + NUM_CUSTOMERS];
string s = "Running " + to_string(threadnum) + " threads in parallel: "; cout << s;
/* spawn Barber threads */ for (int id = 0; id < NUM_BARBERS; id++) threads[id] = thread(Barber, id);
/* spawn Customer threads */ for (int id = 0; id < NUM_CUSTOMERS; id++) threads[id + NUM_BARBERS] = thread(Customer, id + NUM_BARBERS);
/* Merge all threads to the main thread */ for (int id = 0; id < threadnum; id++) threads[id].join();
// WHY ISN'T THIS PRINTING? cout << "Completed barbershop example! "; cout << endl;
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started