Question: Please help me complete Multithreading C + + I am trying to make a simulation of behavior of customers ( patrons ) at a post

Please help me complete
Multithreading C++
I am trying to make a simulation of behavior of customers (patrons)at a post office of up to 128patrons at a post office. Each patron will arrive at the post office, wait for an available clerk, get the help they need, and leave the post office. Each patron will be represented by a separate thread created by a main program. This thread will synchronize with other threads to ensure that each patron will have exclusive access to one of the post office clerks. To achieve this, we must use Pthread mutexes and condition variables. SEMAPHORES NOT ALLOWED.
A txt file will be used to pass through the program.
Arnold 09
Bill 46
Carol 68
Dill 36
The name will never contain spaces, the number of seconds elapsed since the arrival of the previous patron, and the number of seconds the patron will take to get processed by the clerk.
All variables will be shared by all threads must be declared outside of any function: static int nFreeClerks, nPatrons (use these variables instead of what is in the code already).
To pass any data through the thread function, declare them void with the following code:
void *patron(void *arg){
lData =(struct pData) arg;
}//patron
The contents of pData into a local variable
The program should print out a message every time a patron:
1.Arrives at the post office
2.Starts getting help
3.Leaves the post office
At the very end of the program, you should get a display that reads:
1.Total number of patrons served
2.Number of patrons that did not have to wait
3.The number of patrons that waited
#include
#include
#include
#include
#include
struct Patron {
std::string name;
int arrivalTime;
int serviceTime;
};
std::vector patrons;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t clerkAvailable = PTHREAD_COND_INITIALIZER;
int totalServed =0;
int notWaited =0;
int waited =0;
void* clerkFunction(void* arg){
while (true){
pthread_mutex_lock(&mutex);
while (patrons.empty()){
pthread_cond_wait(&clerkAvailable, &mutex);
}
Patron currentPatron = patrons.back();
patrons.pop_back();
pthread_mutex_unlock(&mutex);
std::cout << currentPatron.name <<" starts getting help." << std::endl;
usleep(currentPatron.serviceTime *1000); // Simulate service time
std::cout << currentPatron.name <<" leaves the post office." << std::endl;
totalServed++;
}
return NULL;
}
void* patronFunction(void* arg){
Patron* patron =(Patron*)arg;
usleep(patron->arrivalTime *1000); // Wait until arrival time
pthread_mutex_lock(&mutex);
if (!patrons.empty()){
waited++;
}
else {
notWaited++;
}
patrons.push_back(*patron);
pthread_cond_signal(&clerkAvailable);
pthread_mutex_unlock(&mutex);
std::cout << patron->name <<" arrives at the post office." << std::endl;
return NULL;
}
int main(){
std::ifstream inputFile("input.txt");
if (!inputFile){
std::cerr << "Error opening file!" << std::endl;
return 1;
}
std::string name;
int arrivalTime, serviceTime;
while (inputFile >> name >> arrivalTime >> serviceTime){
patrons.push_back({ name, arrivalTime, serviceTime });
}
pthread_t clerkThread;
pthread_create(&clerkThread, NULL, clerkFunction, NULL);
pthread_t patronThreads[patrons.size()];
for (size_t i =0; i < patrons.size(); ++i){
pthread_create(&patronThreads[i], NULL, patronFunction, (void*)&patrons[i]);
}
for (size_t i =0; i < patrons.size(); ++i){
pthread_join(patronThreads[i], NULL);
}
std::cout << "Total number of patrons served: "<< totalServed << std::endl;
std::cout << "Number of patrons that did not have to wait: "<< notWaited << std::endl;
std::cout << "Number of patrons that waited: "<< waited << std::endl;
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 Accounting Questions!