Today I'd implement a very basic Thread class. My question is why this code run synchronously:
struct Clerk : public Thread
{
Clerk()
{
IsOnService = false;
}
void Run()
{
std::deque<int> dq;
std::cout << this->GetId() << " receive new customer " << '\n';
for (int i = 0; i < 1000000; ++i) dq.push_front(i);
std::cout << this->GetId() << " has finished" << '\n';
IsOnService = false;
}
bool IsOnService;
};
struct Customer
{
bool IsOnService;
};
struct Service
{
Clerk* clerk;
Customer* customer;
Service(Clerk* c, Customer* cu)
{
clerk = c;
customer = cu;
clerk->Join();
}
};
int main()
{
int nClerks = 5;
Clerk* clerks[] = {
new Clerk(), new Clerk(), new Clerk(), new Clerk(), new Clerk()
};
while (1) {
if (GetAsyncKeyState(0x43) & 0x7FFF) {
Customer* newCustomer = new Customer();
for (int i = 0; i < nClerks; ++i)
if (!clerks[i]->IsOnService) {
Service* newService = new Service(clerks[i], newCustomer);
delete newService;
break;
}
}
}
for (int i = 0; i < nClerks; ++i) delete clerks[i];
return 0;
}
First
The deque is only for make a hard work that takes several time, but when I run the code above it takes time but run thread per thread I mean I have something like this when I run it:
C:>100 receive new customer
... several time ...
C:>100 has finished
C:>150 receive new customer
... several time ...
C:>150 has finished
... and so on
And the behavior that I wish to have is the following:
C:>100 receive new customer
C:>150 receive new customer
C:>100 has finished
C:>150 has finished
Or something like that. If someone can help me. I'd used deque because I want to make a task that takes time but the code needs to compile in C++98. Plz don't answer me with code that is implemented in C++11 or higher.