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 patronsat a post office of up to patrons 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
Bill
Carol
Dill
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 patronvoid 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:
Arrives at the post office
Starts getting help
Leaves the post office
At the very end of the program, you should get a display that reads:
Total number of patrons served
Number of patrons that did not have to wait
The number of patrons that waited
#include
#include
#include
#include
#include
struct Patron
std::string name;
int arrivalTime;
int serviceTime;
;
std::vector patrons;
pthreadmutext mutex PTHREADMUTEXINITIALIZER;
pthreadcondt clerkAvailable PTHREADCONDINITIALIZER;
int totalServed ;
int notWaited ;
int waited ;
void clerkFunctionvoid arg
while true
pthreadmutexlock&mutex;
while patronsempty
pthreadcondwait&clerkAvailable, &mutex;
Patron currentPatron patrons.back;
patrons.popback;
pthreadmutexunlock&mutex;
std::cout currentPatron.name starts getting help." std::endl;
usleepcurrentPatronserviceTime ; Simulate service time
std::cout currentPatron.name leaves the post office." std::endl;
totalServed;
return NULL;
void patronFunctionvoid arg
Patron patron Patronarg;
usleeppatronarrivalTime ; Wait until arrival time
pthreadmutexlock&mutex;
if patrons.empty
waited;
else
notWaited;
patrons.pushbackpatron;
pthreadcondsignal&clerkAvailable;
pthreadmutexunlock&mutex;
std::cout patronname arrives at the post office." std::endl;
return NULL;
int main
std::ifstream inputFileinputtxt;
if inputFile
std::cerr "Error opening file!" std::endl;
return ;
std::string name;
int arrivalTime, serviceTime;
while inputFile name arrivalTime serviceTime
patrons.pushback name, arrivalTime, serviceTime ;
pthreadt clerkThread;
pthreadcreate&clerkThread, NULL, clerkFunction, NULL;
pthreadt patronThreadspatronssize;
for sizet i ; i patrons.size; i
pthreadcreate&patronThreadsi NULL, patronFunction, void&patronsi;
for sizet i ; i patrons.size; i
pthreadjoinpatronThreadsi 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 ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
