Question: Please help, im not sure how to fix my code so that it runs properly Here is the instructions: TODO:: Find a free pump and

Please help, im not sure how to fix my code so that it runs properly
Here is the instructions:
TODO:: Find a free pump and fill up using that pump, otherwise wait until a pump becomes
// available.
//
//1) You will be using a single mutex and a 32-bit value to utilize up to 32 different pumps.
// Each bit in the 32-bit 'freemask' will represent the status of a single pump - in use
// or not in use. Refer to the documentation if you need a refresher on how to set, check
// and clear individual bits.
//
//2) All accesses to shared memory must be thread safe and no two cars should even attempt to
// access the same pump at the same time.
//
//3) You will be using the 'fillTankUp' method of the 'Pump' class to simulate a car filling
// up. Filling up must be implemented in such a way that allows for other cars to
// concurrently use the other pumps while a car fills up.
//
//4) If a car fails to find an available pump it must wait until one becomes available.
//
//5) After a car successfully fills up it either must wait for all other cars to fill up OR
// wait until enough time has gone by for all other cars to fill up once. If you choose
// to use the time based algorithm then you'll need to look into the function sleep_for,
// which is found in the std::this_thread namespace. You will need to sleep for a value
// based on the information available to you (number of cars, number of pumps, how long
// a single car takes to fill up).
//
// NOTE: You may NOT modify any of the code outside of the TODOs or add any global or static
// variables UNLESS it's required by your algorithm for #5 above.
Here is my code:
std::unique_lock lock(*getstationMutex());
while (true)
{
//iterate through the number of pumps in the station
for (int i =0; i < pumpsInStation; i++)
{
//check if pump is available
if (freeMask & (1<< i))
{
//mark pump as in use
freeMask &= ~(1<< i);
//unlock mutex while filling up
lock.unlock();
//fill up tank
pumps[i].fillTankUp();
//lock mutex again
lock.lock();
//mark pump as free
freeMask |=(1<< i);
//notify waiting cars
stationCondition->notify_all();
return 0;
}
}
getStationCondition()->wait(lock);
}
return 0;

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!