changing the problem to a car wash problem
This commit is contained in:
parent
c920829d3b
commit
f1b6435b7e
86
main.cpp
86
main.cpp
@ -12,79 +12,79 @@ mutex cout_mutex;
|
|||||||
#define N 25
|
#define N 25
|
||||||
|
|
||||||
// GLOBAL VARIABLES
|
// GLOBAL VARIABLES
|
||||||
int customers = 0;
|
int carsWaiting = 0;
|
||||||
mutex mutexCustomer;
|
mutex mutexCar;
|
||||||
counting_semaphore customer(0);
|
counting_semaphore carArrived(0);
|
||||||
counting_semaphore barber(0);
|
counting_semaphore bayFree(0);
|
||||||
counting_semaphore customerDone(0);
|
counting_semaphore carWashed(0);
|
||||||
counting_semaphore barberDone(0);
|
counting_semaphore washingDone(0);
|
||||||
|
|
||||||
void client(size_t id)
|
void car(size_t id)
|
||||||
{
|
{
|
||||||
// customer enters
|
// car arrives
|
||||||
print("Customer " << id << " enters the barbershop");
|
print("Car " << id << " arrives at the car wash");
|
||||||
mutexCustomer.lock();
|
mutexCar.lock();
|
||||||
if (customers == N) {
|
if (carsWaiting == N) {
|
||||||
mutexCustomer.unlock();
|
mutexCar.unlock();
|
||||||
print("Customer " << id << " bulks");
|
print("Car " << id << " leaves due to no waiting space");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
++customers;
|
++carsWaiting;
|
||||||
mutexCustomer.unlock();
|
mutexCar.unlock();
|
||||||
|
|
||||||
// rendezvous 1
|
// rendezvous 1
|
||||||
customer.release(); // signal X
|
carArrived.release(); // signal X
|
||||||
barber.acquire(); // wait Y
|
bayFree.acquire(); // wait Y
|
||||||
|
|
||||||
// getHairCut
|
// getCarWash
|
||||||
print("Customer " << id << " get his hair cut");
|
print("Car " << id << " is being washed");
|
||||||
this_thread::sleep_for(chrono::seconds(1));
|
this_thread::sleep_for(chrono::seconds(1));
|
||||||
print("Customer " << id << " got a haircut");
|
print("Car " << id << " is washed");
|
||||||
|
|
||||||
// rendezvous 2
|
// rendezvous 2
|
||||||
customerDone.release(); // signal X'
|
carWashed.release(); // signal X'
|
||||||
barberDone.acquire(); // wait Y'
|
washingDone.acquire(); // wait Y'
|
||||||
|
|
||||||
// customer leaves
|
// car leaves
|
||||||
mutexCustomer.lock();
|
mutexCar.lock();
|
||||||
--customers;
|
--carsWaiting;
|
||||||
mutexCustomer.unlock();
|
mutexCar.unlock();
|
||||||
print("Customer " << id << " leaves the barbershop");
|
print("Car " << id << " leaves the car wash");
|
||||||
}
|
}
|
||||||
|
|
||||||
void barber()
|
void attendant()
|
||||||
{
|
{
|
||||||
while (true) {
|
while (true) {
|
||||||
// rendezvous 1
|
// rendezvous 1
|
||||||
customer.acquire(); // wait X
|
carArrived.acquire(); // wait X
|
||||||
barber.release(); // signal Y
|
bayFree.release(); // signal Y
|
||||||
|
|
||||||
// cutHair
|
// washCar
|
||||||
print("Barber starts to cut hair");
|
print("Attendant starts washing a car");
|
||||||
this_thread::sleep_for(chrono::seconds(1));
|
this_thread::sleep_for(chrono::seconds(1));
|
||||||
print("Barber stops to cut hair");
|
print("Attendant finishes washing");
|
||||||
|
|
||||||
// rendezvous 2
|
// rendezvous 2
|
||||||
customerDone.acquire(); // wait X'
|
carWashed.acquire(); // wait X'
|
||||||
barberDone.release(); // signal Y'
|
washingDone.release(); // signal Y'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
print("Barbershop opening with " << N << " chairs");
|
print("Car wash opening with " << N << " waiting spaces");
|
||||||
thread barberThread(barberProcedure);
|
thread attendantThread(attendant);
|
||||||
vector<thread> clients {};
|
vector<thread> cars {};
|
||||||
for(size_t i = 0; i < N+1; ++i)
|
for(size_t i = 0; i < N+1; ++i)
|
||||||
{
|
{
|
||||||
thread t(client, i);
|
thread t(car, i);
|
||||||
clients.push_back(std::move(t));
|
cars.push_back(std::move(t));
|
||||||
}
|
}
|
||||||
|
|
||||||
barber_thread.join();
|
attendantThread.join();
|
||||||
for (auto &client: clients) {
|
for (auto &car: cars) {
|
||||||
client.join();
|
car.join();
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user