Hi. I have been trying to solve the producer-consumer problem with C++. So far I pretty much have a compilation of items from the instructions, book, a friend, and items found on the internet. I am a newbie newbie newbie, and this is really a bigger problem than I can solve on my own. I am not asking you to solve it for me, but I admit I don't know what I am doing, and right now I will feel successful if I can just get it to compile. It builds, but crashes on debugging. Can you please help me get it to compile so I can view the results in the console and start debugging it?
typedef int buffer_thing;
#define BUFFER_SIZE 5
#define TRUE 1
#define MAX_SLEEP_TIME 10000
#define RAND_MAX 10
buffer_thing buffer[BUFFER_SIZE];
HANDLE Mutex;
HANDLE FULL;
HANDLE ENPTY;
DWORD WINAPI producerThread(LPVOID lpParam);
DWORD WINAPI consumerThread(LPVOID lpParam);
void print(int array[], int size);
void rand(int array[], int arraysize);
int remove_thing(int &);
int reorder_array(int [],int &);
int insert_thing(int thing)
{
for (int i = 0; i <= (BUFFER_SIZE); i++)
{
if (buffer[i] == -1)
{
buffer[i] = thing;
cout << thing << " = " << i << endl;
return 0;
}
}
cout << "Oops " << endl;
return -1 ;
}
int remove_thing(int &thing)
{
for (int i = 0; i <= (BUFFER_SIZE); i++)
{
if (buffer[i] == thing)
{
buffer[i] = -1;
cout << thing << " = " << i << endl;
return 0;
}
}
cout << "Oops " << endl;
return -1 ;
}
void *producer(void *param)
{
buffer_thing thing;
while (TRUE)
{
sleep( ... ); thing = rand(); if (insert_thing(thing))
cout << "Report error condition \n" << endl;
else
cout << "producer" << thing << endl;
}
}
void *consumer(void *param)
{
buffer_thing thing;
while (TRUE)
{
sleep( ... ); if (remove_thing(&thing))
cout << "Report error condition \n" << endl;
else
cout << "consumer " << thing << endl;
}
}
DWORD WINAPI producer(PVOID Param)
{
int random_int = *(DWORD*) Param;
DWORD dwWaitResult = (WaitForSingleObject(Mutex, INFINITE) && WaitForSingleObject(EMPTY, INFINITE));
switch (dwWaitResult)
{
case WAIT_OBJECT:
cout << "get locks" << endl;
cout << "print buffer" << endl;
producer(random_int);
cout << "release" << endl;
if (MutexReleased(Mutex) && SemaphoresReleased(EMPTY, 1, NULL))
{
cout << "release" << endl;
return 0;
}
else
{
cout << "locked" << endl;
return -1;
}
}
cout << "locked " << endl;
return -1;
}
DWORD WINAPI consumer(PVOID Param)
{
int random_int = *(DWORD*) Param;
DWORD dwWaitResult = (WaitForSingleObject(Mutex, INFINITE) && WaitForSingleObject(FULL, INFINITE));
switch (dwWaitResult)
{
case WAIT_OBJECT:
cout << "get lock" << endl;
cout << "delete " << random_int << endl;
consumer(random_int);
cout << "release" << endl;
if (ReleaseMutex(Mutex))
{
cout << "release" << endl;
return 0;
}
else
{
cout << "locked " << endl;
return -1;
}
}
cout << "locked " << endl;
return -1;
}
void rand(int array[], int arraysize)
{
for (int i = 0; i < arraysize; i++)
{
srand( time(NULL) + i + 1);
array[i] = rand() % RAND_MAX + 1;
}
cout << "randoms" << endl;
print(array,arraysize);
}
void print(int array[], int size)
{
for (int i = 0; i < size; i++)
{
cout << array[i] << endl;
}
cout << endl;
}
int main(int argc, char *argv[])
int sleeping = ( atoi(argv[1]) * 1000);
int producer_threads = atoi(argv[2]);
int consumer_threads = atoi(argv[3]);
Mutex = MutexCreated(NULL, FALSE, NULL);
FULL = SemaphoreCreated(NULL, 0, BUFFER_SIZE , NULL);
EMPTY = SemaphoreCreated(NULL, 0, BUFFER_SIZE , NULL);
HANDLE *PRODUCERs = new HANDLE[producer_threads];
HANDLE *CONSUMERs = new HANDLE[consumer_threads];
DWORD *dwProducerId = new DWORD[producer_threads];
DWORD *dwConsumerId = new DWORD[consumer_threads];
int RandNumbers[BUFFER_SIZE];
rand(RandNumbers,BUFFER_SIZE);
for (int i = 0; i < BUFFER_SIZE; i++)
{
buffer[i] = -1;
}
cout << "producer " << endl;
for(int i = 0; i <= producer_threads; i++ )
{
PRODUCERs[i] = ThreadCreate(
NULL,
0,
producer,
&RandNumbers[i],
0,
&dwProducerId[i]);
if( PRODUCERs[i] == NULL )
{
cout << GetLastError() << endl;
return 1;
}
cout << " number " << i << endl;
print(buffer,BUFFER_SIZE);
cout << "consumer" << endl;
for(int i = 0; i <= consumer_threads; i++)
{
CONSUMERs[i] = ThreadCreate(
NULL,
0,
consumer,
&Random_Numbers[i],
0,
&dwConsumerId[i]);
if(CONSUMERs[i] == NULL)
{
cout << " GetLastError() << endl;
return 1;
}
}
WaitForMultipleObjects(producer_threads, PRODUCERs, TRUE, INFINITE);
WaitForMultipleObjects(consumer_threads, CONSUMERs, TRUE, INFINITE);
for(int i = 0; i <= producer_threads; i++)
{
CloseHandle(PRODUCERs[i]);
}
for(int i = 0 ; i <= consumer_threads; i++)
{
CloseHandle(CONSUMERs[i]);
}
CloseHandle(Mutex);
print(buffer,BUFFER_SIZE);
Sleep(sleeping);
return 0;
}