I have created the following code to act as a buffer between two threads. I am trying to add support to handle empty and or full buffers, and am not sure how to address this. Would it be done through condition variables? Thanks.

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>                     //for sleep() if needed

const int N = 100000000;                        // buffer size
int Buffer[N];
int NumOccupied = 0;                    // Number of items currently in Buffer
int FirstOccupied = 0;                  // Index to first (next) item in Buffer to add/remove

pthread_mutex_t BuffMutt = PTHREAD_MUTEX_INITIALIZER;

// Constraint: To start, will assume that N values will be added or read!!

void buffadd(int value)
{
	pthread_mutex_lock(&BuffMutt);
	Buffer[FirstOccupied + NumOccupied] = value;
	//pthread_yield();
	//sleep(1);
	++NumOccupied;
	pthread_mutex_unlock(&BuffMutt);
}

int buffrem()
{
	int value;
	
	pthread_mutex_lock(&BuffMutt);
	value = Buffer[FirstOccupied];
	++FirstOccupied;
	--NumOccupied;
	pthread_mutex_unlock(&BuffMutt);
	return(value);
}

void *tcode1(void *empty)
{
	//Simply adds ten values to the buffer
	int i;
	for(i = 0; i<N;i++)
		buffadd(i);
}

void *tcode2(void *empty)
{
	//Reads values off buffer and confirms the values
	int i, val;
	for(i = 0; i<N; i++)
	{
		val = buffrem();
		if(val != i)
		{
			printf("removed %d, expected %d\n", val, i);
		}
	}
}

main()
{
	pthread_t t1, t2;
	
	pthread_create(&t1, NULL, tcode1, NULL);
	pthread_create(&t2, NULL, tcode2, NULL);
	
	pthread_join(t1, NULL);
	pthread_join(t2, NULL);
}

Thank you. So basically every time that I unlock/lock mutex I have to do the following with the condition variable?

Just want to make sure that I understand completely... Am I correct?

//This program expands the created bounded buffer, to improve functionality when encountering an empty 
//or full buffer. "nLimit" sets the maxiumum buffer size. When this size is reached, the buffer is full. 
//When the buffer is either full or empty, a message is displayed to the user via command line. 
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>                     //for sleep() if needed
#include <stdlib.h>

const int N = 1000;                        // buffer size
int nLimit = 10000;						//buffer limit 
int Buffer[N];
int NumOccupied = 0;                    // Number of items currently in Buffer
int FirstOccupied = 0;                  // Index to first (next) item in Buffer to add/remove

int NUMTHREADS = 2;
//pthread_mutex_t count = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t m_mutex;
pthread_cond_t c_threshold_cv;

// Constraint: To start, will assume that N values will be added or read!!
void buffadd(int value)
{
	pthread_mutex_lock(&m_mutex);
	Buffer[FirstOccupied + NumOccupied] = value;
	//pthread_yield();
	//sleep(1);
	++NumOccupied;
	//condition variable to respnd with a message that the buffer is full if the limit is reached
	if (value>= nLimit)
	{
		pthread_cond_signal(&c_threshold_cv);
		printf("Threshold reached\n");
	}
	pthread_mutex_unlock(&m_mutex);
}

int buffrem()
{
	int value;
	pthread_mutex_lock(&m_mutex);
	value = Buffer[FirstOccupied];
	++FirstOccupied;
	--NumOccupied;
	//condition varaible to respond with a message that the buffer is empty if it in fact is. 
	if (NumOccupied == 0)
	{
		pthread_cond_signal(&c_threshold_cv);
		printf("Empty Buffer\n");
	}
	pthread_mutex_unlock(&m_mutex);
	return(value);
}

void *tcode1(void *empty)
{
	//Simply adds ten values to the buffer
	int i;
	for(i = 0; i<N;i++)
		buffadd(i);
}

void *tcode2(void *empty)
{
	//Reads values off buffer and confirms the values
	int i, val;
	for(i = 0; i<N; i++)
	{
		val = buffrem();
		if(val != i)
		{
			printf("removed %d, expected %d\n", val, i);
		}
	}
}

int main()
{
	pthread_t t1, t2;
	int i = 0;
	pthread_t threads[2];
	pthread_attr_t attr;
	
	// Initialize mutex and condition variable objects
	pthread_mutex_init(&m_mutex, NULL);
	pthread_cond_init (&c_threshold_cv, NULL);
	
	// For portability, explicitly create threads in a joinable state
	pthread_attr_init(&attr);
	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
	pthread_create(&t1, NULL, tcode1, NULL);
	pthread_create(&t2, NULL, tcode2, NULL);

	//Wait for all threads to complete
	for (i=0; i<2; i++) {
		pthread_join(threads[i], NULL);
	}
	
	// Clean up
	pthread_attr_destroy(&attr);
	pthread_mutex_destroy(&m_mutex);
	pthread_cond_destroy(&c_threshold_cv);
pthread_exit(NULL);}
This article has been dead for over six months. Start a new discussion instead.