pthread condition variables

This is a discussion on pthread condition variables within the C Programming forums, part of the General Programming Boards category; I am creating a ringbuffer that supports multithreaded calls from both consumers and producers, and, while I feel fairly confident ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    9

    pthread condition variables

    I am creating a ringbuffer that supports multithreaded calls from both consumers and producers, and, while I feel fairly confident that the code looks right, when I run it I find (with a debugging trace statement) that it is actually running gets when the buffer is empty, or puts when the buffer is full. Please look over the pthread condition variables and let me know if you see something wrong.

    Code:
    void 
    ringbuf_put(ringbuf_t *rb,value_type y)
    {
    	pid=pthread_self();
    	pthread_mutex_lock(&mtx);
    	while (rb->size>=rb->capacity)
    	{
    		pthread_cond_wait(&not_full, &mtx);
    		rb->buf[rb->tail]=y;
        if (rb->tail==rb->capacity)
    		rb->tail=0;
    	rb->tail++;
    	rb->size++;
    	//printf("PUT, %d,  %d, %d, %d, %d, Inside condition\n", pid, cid, rb->size, rb->capacity, y);
    	if (rb->size>10)
    	{pthread_cond_signal(&not_empty);}
    	pthread_mutex_unlock(&mtx);
    	return;
    	}
        if (rb->size<rb->capacity)
    	{
    		rb->buf[rb->tail]=y;
        if (rb->tail==rb->capacity)
    		rb->tail=0;
    	rb->tail++;
    	rb->size++;
    	//printf("PUT, %d,  %d, %d, %d, %d\n", pid, cid, rb->size, rb->capacity, y);
    	if (rb->size>10)
    	{pthread_cond_signal(&not_empty);}
    	pthread_mutex_unlock(&mtx);
    	return;
    	}
    	printf("failed PUT call\n");
    }
    
    
    value_type 
    ringbuf_get(ringbuf_t *rb)
    {
    	value_type x;
    
    	cid=pthread_self();
    		pthread_mutex_lock(&mtx);
    		while (rb->size <= 0)
    	{
    		pthread_cond_wait(&not_empty, &mtx);
    		x = rb->buf[rb->head];
    		if (rb->head==rb->capacity)
    			rb->head=0;
    		else
    			rb->head++;
    		rb->size--;
    	//	printf("GET, %d,  %d, %d, %d, %d, Inside condition\n", pid, cid, rb->size, rb->capacity, rb->buf[rb->head]);
    		if (rb->size<(rb->capacity-10))
    		{pthread_cond_signal(&not_full);}
    		pthread_mutex_unlock(&mtx);
    		return x;
    	}
    	if (rb->size > 0)
    	{
    	x = rb->buf[rb->head];
    	if (rb->head==rb->capacity)
    		rb->head=0;
    	else
    		rb->head++;
    	rb->size--;
       // printf("GET, %d,  %d, %d, %d, %d\n", pid, cid, rb->size, rb->capacity, rb->buf[rb->head]);
    	if (rb->size<(rb->capacity-10))
    	{pthread_cond_signal(&not_full);}
    	pthread_mutex_unlock(&mtx);
    	return x;}
    		printf("failed GET call\n");
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Right, so you're showing us two functions that are being called. How about showing us the part of the code that is actually calling them. That is the problem after all, right?

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Father and Son Variables
    By khdani in forum Linux Programming
    Replies: 3
    Last Post: 11-28-2008, 05:42 PM
  2. Remotely Creating Variables
    By Rajin in forum C++ Programming
    Replies: 1
    Last Post: 04-26-2005, 11:20 PM
  3. Declaring an variable number of variables
    By Decrypt in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 03:46 PM
  4. hwnd and variables in them
    By underthesun in forum Windows Programming
    Replies: 6
    Last Post: 01-16-2005, 05:39 PM
  5. functions to return 2 variables?
    By tim in forum C Programming
    Replies: 5
    Last Post: 02-18-2002, 01:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21