Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 901829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:37:11+00:00 2026-05-15T15:37:11+00:00

I am using the pthread library to simulate a threaded buffer. I am also

  • 0

I am using the pthread library to simulate a threaded buffer. I am also using semaphores as a solution to accessing critical section variables one at a time.

The main problem is that the producer is filling the entire buffer and the consumer is then emptying the entire buffer. Is this code correct? I was assuming that the production and consumption would occur before the buffer was full or empty.

Here is my code and any comments would help a lot, and yes this is for a class.

Thank you in advance

void *Producer(void *threadid) 
{
   long tid;
   tid = (long)threadid;

   while (c < Cycles)  //While stuff to buffer
   {                        
        pthread_mutex_lock(&lock);           
        while(size == BUFFER_SIZE)  
        {
           pthread_cond_wait(&cond, &lock);    
        }
     buffer [full] = rand();
     data << size+1 << ". Produce: " << buffer[full] << endl;
     printBuffer();
     full = (full + 1) % BUFFER_SIZE;
     size++;
     pthread_cond_signal(&cond1);
     pthread_mutex_unlock(&lock);
     c++;
   }
   pthread_exit(NULL);
}

You can also download all the code or see the log file…

download main.cpp
view the log file at funkohland.com/pthreads/log.txt

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T15:37:12+00:00Added an answer on May 15, 2026 at 3:37 pm

    This is a well known problem with Mutexes. A Mutex is an expensive operation that requires lots of cycles. When you unlock the mutex the other thread has a TINY opportunity in which to exit its lock and gain a lock. Basially you need to spend less time in the mutex to give the other thread an opportunity to run. Basically you need to choose the portion of code that ACTUALLY required the mutex and then lock the mutex quickly do whatever you need to do with that variable (and nothing more) and then unlock it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using pthread on Linux. I have a circular buffer to pass data from
We have an application that creates several threads using the pthread library. As we
Is there a way while using pthread.h on a Linux GCC to keep variables
I load the pthread library in my program using dlopen , and fetch handlers
I am using pthread library for multi-threading. Inside thread function, I use sleep system
Using the pthread library in C, is it possible to send a SIGSTOP signal
I've been using the pthread library for creating & joining threads in C. When
I am using pthread library under Linux for creating threads and I have two
If I create thread using Pthread library under Linux, I need to use function
I'm using pthread on Windows Form in Visual Studio 2008 Professional, But I'm getting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.