Question: please can someone fix this problem for me and run the code to see the output thank you #include #include #include #include #define MAX _

please can someone fix this problem for me and run the code to see the output thank you
#include
#include
#include
#include
#define MAX_THREADS 2// # of threads
using namespace std;
// initialize the variables
int counterLimit =0;
int counter =0;
int bufferSize;
int *buffer;
int in =0;
int out =0;
// function prototypes
DWORD WINAPI producerThread(LPVOID);
DWORD WINAPI consumerThread(LPVOID);
HANDLE hThreads[MAX_THREADS]; // # of threads
DWORD id[MAX_THREADS]; // array of thread ids
DWORD waiter;
// main function
int main()
{
srand(time(0)); // initialize randomization
// read the buffer size
cout "Enter the buffer size: ";
cin >> bufferSize;
// allocate memory fo the buffer
buffer =(int *) malloc(bufferSize * sizeof(int));
// read counter limit
cout "Enter the counter limit: ";
cin >> counterLimit;
cout endl;
// create the threads
hThreads[0]= CreateThread(NULL,0, producerThread, (LPVOID)0, NULL, &id[0]); //thread 1 for random producer
hThreads[1]= CreateThread(NULL,0, consumerThread, (LPVOID)0, NULL, &id[1]); //thread 2 for random consumer
waiter = WaitForMultipleObjects(MAX_THREADS, hThreads, TRUE, INFINITE);
for(int i =0; i MAX_THREADS; i++)
{
CloseHandle(hThreads[i]);
}
// print status of threads
cout "
Thread ended (producerThread)..." endl;
cout "Thread started (consumerThread)...
" endl;
system("pause");
return 0;
}
// producer thread
DWORD WINAPI producerThread(LPVOID n)
{
cout "Thread started (producerThread)...
" endl;
while(counter counterLimit && counter bufferSize)
{
while(((in +1)% bufferSize)== out);
int nextProduced = rand()%10+1;
Sleep(nextProduced *400);
buffer[in]= nextProduced;
cout "producerThread has produced " nextProduced endl;
in =(in +1)% bufferSize;
counter++;
}
return (DWORD)n;
}
// consumer thread
DWORD WINAPI consumerThread(LPVOID n)
{
cout "Thread started (consumerThread)..." endl;
int counter =0;
while (counter counterLimit && counter bufferSize)
{
while(in == out);
Sleep(rand()%400);
int nextConsumed = buffer[out];
cout "consumerThread has consumed " nextConsumed endl;
out =(out +1)% bufferSize;
counter++;
}
if (counterLimit > bufferSize)
{
cout "The buffer is full." endl;
}
return (DWORD)n;
}
 please can someone fix this problem for me and run the

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 Databases Questions!