This is a simple example to test and show VR-Link's threading classes. This example is composed of several classes, and several functions. The central function is called runTest().
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <iostream>
#include <set>
#include <stack>
void print_ts(const char* msg)
{
std::cout << msg << std::endl;
}
class Restaurant;
class Customer;
class Order;
class Food;
class Order
{
public:
int timeToCook;
Customer* theCustomer;
};
class Food
{
public:
int timeToEat;
};
{
public:
Restaurant();
void orderFood(Order order);
void enter(Customer* customer);
void leave(Customer* customer);
protected:
bool noCustomers();
Order takeOrder();
Food makeFood(Order order);
void takeBreak();
void close();
protected:
CustomerSet myCustomers;
OrderStack myOrders;
};
{
public:
Customer(const char* name,int time,Restaurant* res);
void serveFood(Food food);
const char* name() const;
protected:
void orderFood();
void waitForFood();
void eatFood();
void leave();
protected:
const char* myName;
int myTime;
FoodStack myFoodStack;
Restaurant* myRestaurant;
};
Customer::Customer(const char* name,int time,Restaurant* res) :
myName(name),
myTime(time),
myFoodStack(std::stack<Food>()),
myRestaurant(res)
{
res->enter(this);
}
void Customer::run()
{
orderFood();
waitForFood();
eatFood();
leave();
};
const char* Customer::name() const
{
return myName;
}
void Customer::orderFood()
{
char buffer[256];
sprintf(buffer,"%s: ordering food.",myName);
print_ts(buffer);
Order order;
order.timeToCook = myTime;
order.theCustomer = this;
myRestaurant->orderFood(order);
}
void Customer::waitForFood()
{
while(myFoodStack->size() == 0)
DtSleep(.5);
}
void Customer::eatFood()
{
Food food = myFoodStack->top();
myFoodStack->pop();
for(int i = 0;i < food.timeToEat;++i)
{
char buffer[256];
sprintf(buffer,"%s: Eating for %d",myName,i);
print_ts(buffer);
}
}
void Customer::serveFood(Food food)
{
myFoodStack->push(food);
}
void Customer::leave()
{
char buffer[256];
sprintf(buffer,"%s: leaving restaurant",myName);
print_ts(buffer);
myRestaurant->leave(this);
}
Restaurant::Restaurant() :
myOrders(std::stack<Order>()),
myCustomers(std::set<Customer*>())
{
}
void Restaurant::run()
{
while(true)
{
if(noCustomers())
{
close();
return;
}
Order order = takeOrder();
if(order.theCustomer != 0)
{
Food meal = makeFood(order);
print_ts("Restaurant: Serving food");
order.theCustomer->serveFood(meal);
}
else
{
takeBreak();
}
}
close();
}
bool Restaurant::noCustomers()
{
return myCustomers->size() == 0;
}
void Restaurant::orderFood(Order order)
{
myOrders->push(order);
}
Order Restaurant::takeOrder()
{
Order order;
order.theCustomer = 0;
{
if(myOrders->size() > 0)
{
order = myOrders->top();
myOrders->pop();
print_ts("Restaurant: Took Order");
}
}
return order;
}
Food Restaurant::makeFood(Order order)
{
char buffer[256];
int totalTime = order.timeToCook;
while(totalTime > 0)
{
sprintf(buffer,"Restaurant: Making food: Finished in %d s",totalTime);
print_ts(buffer);
--totalTime;
}
Food food;
food.timeToEat = order.timeToCook * 2;
return food;
}
void Restaurant::leave(Customer* customer)
{
myCustomers->erase(customer);
}
void Restaurant::enter(Customer* customer)
{
char buffer[256];
sprintf(buffer,"Restaurant: Seeting %s",customer->name());
print_ts(buffer);
myCustomers->insert(customer);
}
void Restaurant::takeBreak()
{
print_ts("Restaurant: Taking 5");
}
void Restaurant::close()
{
print_ts("Restaurant: Closing");
}
void RunTest()
{
const char* customerNames[] =
{
"Colin",
"Jim",
"Len",
"Howard",
"Mike",
"Karen",
"Jeff",
"Jeff",
"Ben",
"Rob",
"Marshal",
"Doug",
"Aline",
"Nate",
"Fred",
"Jorge"
};
int customerCount = 16;
time_t t = time(NULL);
srand(t);
Restaurant* aRestaurant = new Restaurant();
Customer* customer1 = new Customer(customerNames[rand() % customerCount],2,aRestaurant);
Customer* customer2 = new Customer(customerNames[rand() % customerCount],3,aRestaurant);
Customer* customer3 = new Customer(customerNames[rand() % customerCount],4,aRestaurant);
Customer* customer4 = new Customer(customerNames[rand() % customerCount],5,aRestaurant);
Customer* customer5 = new Customer(customerNames[rand() % customerCount],6,aRestaurant);
thread0.start();
thread1.start();
thread2.start();
thread3.start();
thread4.start();
thread5.start();
thread1.join();
thread2.join();
thread3.join();
thread4.join();
thread5.join();
thread0.join();
}
int main(int argc, char* argv[])
{
RunTest();
}