#include <cstdarg>
#include <Windows.h>
#include <iostream>
#include <cmath>
#include <ctime>
#define MAX_COUNT 10//The largest number of barber
Total number of chairs in the #define chairs 4//Shop
using namespace Std;
intwaiting=0; Number of customers waiting for a haircut
Char Close_door; Close the door
int count=0; Customer's serial number
int finish=0; Number of customers who have finished their manager's hair
HANDLE Mutex =createmutex (NULL, FALSE, "mutex"); The mutex used to implement the process
HANDLE Barbers =createsemaphore (NULL, 1,1, "barbers"); Defines the semaphore for synchronization between threads
HANDLE Customers =createsemaphore (Null,0,chairs, "customers"); Defines the semaphore for synchronization between threads
int random ()//defines a random function to produce a customer and makes the time between two customers less than 10 seconds
{
Srand ((int) time (NULL));
Return to Rand ()%5000;
}
DWORD WINAPI Customer (LPVOID pParm2)//Customer thread
{
if (ReleaseSemaphore (customers,1,null))//v (customer)
{
WaitForSingleObject (Mutex, INFINITE);
count++;
cout<< "You are the first" <<count<< "customer, welcome your arrival ^_^" <<endl;
if (waiting!=0)
{
cout<< "There are now <<waiting << customers waiting for a haircut, please wait patiently ^_^" <<endl;
}
Else
cout<< "No customers in the barber, I immediately serve you ^_^" <<endl;//output How many people are waiting
waiting++;
ResumeThread (customers);/Wake the Barber process
ReleaseMutex (mutex);//frees the mutex so that other threads can use the
WaitForSingleObject (Barbers,infinite); Waiting for a haircut
}
Else
{
count++;
cout<< "Sorry, no empty chairs ... section "<<count<<" a customer leaves the barber shop <<endl; Without a chair, the customer leaves directly.
}
return 0;
}
DWORD winapi Barber (LPVOID pParm1)/Barber thread
{
while (true)//outer loop
{
WaitForSingleObject (Customers,infinite);//p (Customers), waiting for customers
WaitForSingleObject (Mutex,infinite); Wait for mutex quantity
ReleaseSemaphore (Barbers,1,null); Release semaphore
ResumeThread (barbers); Wake up the customer process
Sleep (5000); Simulated Barber www.bianceng.cn
finish++; The number of customers who have finished a haircut plus 1
cout<< "First" <<finish<< "Customer Barber finished, leave" <<endl;
waiting--; The number of people waiting is reduced by 1
ReleaseMutex (Mutex); V (mutex);
}
return 0;
}
int main ()//To implement the operation of the thread
{
cout<< "*************** new store opened, warmly welcome customers to visit the Everbright!! "<<endl;
cout<< "We have a total of" <<CHAIRS<< "chairs" <<endl;
HANDLE Hthreadcustomer;
HANDLE Hthreadbarder;
Hthreadbarder=createthread (Null,0,barber,null,0,null); Produce a barber process
while (close_door!= ' Y ')
{
Sleep (random ());//rand () function realizes random arrival of customers
cout<<endl<< "is open, please come in!" "<<endl;
if (finish>=max_count)//If the completion number exceeds 8 and no one waits
{
while (waiting!=0)
{
Sleep (1000);
Continue
}
cout<< "has had a haircut for the <<finish<< customer, is it closed?" <<endl; Tip whether to close the door
cin>>close_door;
if (close_door== ' y ')
{
cout<< "Suspend business! Welcome to the next visit! "<<endl;
System ("pause");
return 0;
}
Else
{
finish=0;
count=0;
cout<< "Continue business" <<endl;
}
}
Hthreadcustomer=createthread (Null,0,customer,null,0,null);
}
return 0;
}