Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?

C++
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( ... );		//sleep for a random period of time
		thing = rand();		//generate a random number
		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( ... );	//sleep for a random period of time
		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;
}
Posted
Comments
Richard MacCutchan 30-May-13 3:33am    
You first need to show which lines are giving problems, and what error messages are associated with them.
GigTu 30-May-13 9:33am    
I don't get any error per se. It dies at int main() which simply means, it doesn't compile and won't let me step over to break it down. And I don't know enough to answer any deeper. So after all the work I've done, it's exactly as if I've done nothing. I can just steal someone's code off the net and alter it, but I'm trying to build my own. No one can look at it and spot an error, or is willing to show me how to get it going, or test it to tell me what I need to change?
[no name] 30-May-13 9:38am    
If it does not compile, it is not possible that it "crashes while debugging" as there is nothing to debug. You need to fix whatever the problems are that are preventing the program from compiling to begin with. You need to supply a lot more information for someone to help you like what the errors are would be a good start.
Richard MacCutchan 30-May-13 11:53am    
I don't know how you can say that, when this code will not even compile. Fix your source code errors first.
KarstenK 30-May-13 9:45am    
What youre doing isnt fine. Be clearer and preciser.

What are the message in "dont compile".

Use my tips!!!! Write an empty program and add one feature after an other. And only if its works.

1 solution

I will give you some tips:

a) split your program in solvable tasks and solve them
b) make outputs after every important step or error
c) learn the language, study interesting examples
d) ask clear and short questions
e) dont stop tryin'

"Have al of of fun" ;-)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900