Can you convert this from C++ to C? #include <stdio.h> //#include \"stdafx.h\" #
ID: 3915110 • Letter: C
Question
Can you convert this from C++ to C?
#include <stdio.h>
//#include "stdafx.h"
#include <iostream>
#include <pthread.h>
#include <thread>
#include <queue>
#include <mutex>
using namespace std;
queue<int> patients;
int seats = 0;
//mutex to ensure that only one patient is consulted at one time
mutex doct;
//mutex to ensure that the seats are occupied one after the other
mutex sts;
void doctorState()
{
while (true)
{
if (!patients.empty())
{
int r = (rand() % 10) + 1;
cout << " Patient " << patients.front() << " is getting treatment for " << r << " seconds.";
this_thread::sleep_for(std::chrono::seconds(r));
patients.pop();
break;
}
else
{
cout << " Doctor is sleeping.";
break;
}
}
}
void patientHandler(int i)
{
while (true)
{
if (doct.try_lock())
{
doct.lock();
sts.lock();
seats--;
sts.unlock();
doctorState();
doct.unlock();
break;
}
else if (seats < 3)
{
sts.lock();
seats++;
cout << " Patient " << i << " waiting. Seats Occupied: " << seats;
sts.unlock();
}
else
{
int r = (rand() % i) + 1;
cout << " Patient " << i << " drinking coffee for " << r << " seconds.";
this_thread::sleep_for(std::chrono::seconds(r));
}
}
}
int main()
{
printf(" ");
int size, i;
cout << "Please enter number of patients: ";
cin >> size;
thread doct(doctorState);
thread p[size];
for (i = 0; i < size; i++)
{
//p[i] = std::thread(patientHandler, i);
p[i] = thread(patientHandler, i+1);
patients.push(i);
}
for(i = 0; i < size; i++)
{
p[i].join();
}
doct.join();
printf(" Program completed ");
}
Explanation / Answer
#include <stdio.h>
#include <pthread.h>
#include <thread>
#include <queue>
#include <mutex>
using namespace std;
queue<int> patients;
int seats = 0;
//mutex to ensure that only one patient is consulted at one time
mutex doct;
//mutex to ensure that the seats are occupied one after the other
mutex sts;
void doctorState()
{
while (true)
{
if (!patients.empty())
{
int r = (rand() % 10) + 1;
printf( " Patient %d is getting treatment for %d seconds.", patients.front(), r);
this_thread::sleep_for(std::chrono::seconds(r));
patients.pop();
break;
}
else
{
printf( " Doctor is sleeping.");
break;
}
}
}
void patientHandler(int i)
{
while (true)
{
if (doct.try_lock())
{
doct.lock();
sts.lock();
seats--;
sts.unlock();
doctorState();
doct.unlock();
break;
}
else if (seats < 3)
{
sts.lock();
seats++;
printf( " Patient %d waiting. Seats Occupied: %d", i , seats);
sts.unlock();
}
else
{
int r = (rand() % i) + 1;
printf(" Patient %d drinking coffee for seconds. ", i, r);
this_thread::sleep_for(std::chrono::seconds(r));
}
}
}
int main()
{
printf(" ");
int size, i;
printf("Please enter number of patients: ");
scanf("%d", &size);
thread doct(doctorState);
thread p[size];
for (i = 0; i < size; i++)
{
//p[i] = std::thread(patientHandler, i);
p[i] = thread(patientHandler, i+1);
patients.push(i);
}
for(i = 0; i < size; i++)
{
p[i].join();
}
doct.join();
printf(" Program completed ");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.