I'm doing a pizzeria restaurant simulation, where the main thread is the Reception, it can take orders and transform it in abstract pizza object APizza, in this reception i have a kitchen object, which contain a list of cooks represented by threads (std::list< std::thread > _cooks) i want to pass theses abstract pizzas from the reception main thread, to my sub-threads cooks, and i want them to be able to pick from the ingredient stock (Stock *_ingredients) in the kitchen main threads, i know i must use mutex to lock ingredients stock variable before modifying it so many cooks won't access and change the data in the stock at the same time do unpredictable behavior.
I'm looking for some approach to passe theses pizzas and to make my main thread kitchen ingredient stock (Stock *_ingredients) stock accessible from the cooks.
Here's my architecture:
class Reception
{
public:
Reception(double, size_t, size_t);
~Reception();
int Shell();
APizza *MakeOrder(PizzaType, PizzaSize);
void openKitchen();
private:
int parseCommands();
std::map<size_t, pid_t> _kitchenList;
protected:
double _multiplier; //Cooking time multiplier.
size_t _cooks; //Number of cook(s) per kitchen.
size_t _restock; //Time in ms to restock ingredients.
};
class Kitchen
{
public:
Kitchen(double, size_t, size_t);
~Kitchen();
APizza *MakeOrder(PizzaType, PizzaSize);
void Status();
void DispatchPizza(APizza *pizza);
bool isFull();
private:
std::stack<APizza> \
_pizzaWaiting;
std::list<std::thread> _cooks;
Stock *_ingredients;
double _multiplier; //Cooking time multiplier.
size_t _ncooks; //Number of cook(s) per kitchen.
size_t _restock; //Time in ms to restock ingredients.
size_t _ordersNow;
//Pipe _pipe;
};
class Cook
{
public:
Cook(double _multiplier);
~Cook();
void Run();
bool canCook();
void cookPizza(APizza *);
private:
APizza *_currPizza;
bool _isCooking;
double _multiplier;
};
Here is where i want to pass the pizza
int Reception::Shell()
{
std::string command;
std::cout << "> ";
std::list<std::string> orders;
APizza *currPizza;
>> Kitchen *kitch = new Kitchen(_multiplier, _cooks, _restock);
while (1)
{
getline (std::cin, command);
if (!command.compare("exit") || std::cin.eof())
return(0);
else
{
orders = splitStr(command, ';');
}
if (!orders.empty())
{
for (const std::string & order : orders)
{
size_t nPizza = getPizzaNbr(order);
PizzaType tPizza = getPizzaType(order);
PizzaSize sPizza = getPizzaSize(order);
if (nPizza == 0 || tPizza == (PizzaType)0 || sPizza == (PizzaSize)0) {
std::cout << "wrong input: " << nPizza << " " << tPizza << " " << sPizza << std::endl;
continue;
}
std::cout << "good input: " << nPizza << " " << tPizza << " " << sPizza << std::endl;
for (size_t i = 0; i != nPizza; i++)
{
currPizza = this->MakeOrder(tPizza, sPizza);
//
// SEND this currPizza to to kitchen cook's thread
//
std::cout << "Nouvelle pizza, type: " << currPizza->getType() \
<< " size: " << currPizza->getSize() << std::endl;
}
}
}
std::cout << std::endl << "> ";
}
return 0;
}