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

  • Home
  • SEARCH
  • 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 67157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:12:57+00:00 2026-05-10T19:12:57+00:00

I would like to implement a producer/consumer scenario that obeys interfaces that are roughly:

  • 0

I would like to implement a producer/consumer scenario that obeys interfaces that are roughly:

class Consumer { private:     vector<char> read(size_t n) {         // If the internal buffer has `n` elements, then dequeue them         // Otherwise wait for more data and try again     } public:     void run() {         read(10);         read(4839);         // etc     }     void feed(const vector<char> &more) {         // Safely queue the data         // Notify `read` that there is now more data     } }; 

In this case, feed and run will run on separate threads and read should be a blocking read (like recv and fread). Obviously, I will need some kind of mutual exclusion on my deque, and I will need some kind of notification system to inform read to try again.

I hear condition variables are the way to go, but all my multithreading experience lies with Windows and am having a hard time wrapping my head around them.

Thanks for any help!

(Yes, I know it’s inefficient to return vectors. Let’s not get into that.)

  • 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. 2026-05-10T19:12:58+00:00Added an answer on May 10, 2026 at 7:12 pm

    This code is not production ready. No error checking is done on the results of any library calls.

    I have wrapped the lock/unlock of the mutex in LockThread so it is exception safe. But that’s about it.

    In addition if I was doing this seriously I would wrap the mutex and condition variables inside objects so they can cot be abused inside other methods of Consumer. But as long as you take note that the lock must be acquired before you use the condition variable (in any way) then this simple situation can stand as is.

    Out of interest have you checked the boost threading library?

    #include <iostream> #include <vector> #include <pthread.h>  class LockThread {     public:     LockThread(pthread_mutex_t& m)         :mutex(m)     {         pthread_mutex_lock(&mutex);     }     ~LockThread()     {         pthread_mutex_unlock(&mutex);     }     private:         pthread_mutex_t& mutex; }; class Consumer {     pthread_mutex_t     lock;     pthread_cond_t      cond;     std::vector<char>   unreadData;     public:     Consumer()     {         pthread_mutex_init(&lock,NULL);         pthread_cond_init(&cond,NULL);     }     ~Consumer()     {         pthread_cond_destroy(&cond);         pthread_mutex_destroy(&lock);     }      private:         std::vector<char> read(size_t n)         {             LockThread  locker(lock);             while (unreadData.size() < n)             {                 // Must wait until we have n char.                 // This is a while loop because feed may not put enough in.                  // pthread_cond() releases the lock.                 // Thread will not be allowed to continue until                 // signal is called and this thread reacquires the lock.                  pthread_cond_wait(&cond,&lock);                  // Once released from the condition you will have re-aquired the lock.                 // Thus feed() must have exited and released the lock first.             }              /*              * Not sure if this is exactly what you wanted.              * But the data is copied out of the thread safe buffer              * into something that can be returned.              */             std::vector<char>   result(n); // init result with size n             std::copy(&unreadData[0],                       &unreadData[n],                       &result[0]);              unreadData.erase(unreadData.begin(),                              unreadData.begin() + n);             return (result);         } public:     void run()     {         read(10);         read(4839);         // etc     }     void feed(const std::vector<char> &more)     {         LockThread  locker(lock);          // Once we acquire the lock we can safely modify the buffer.         std::copy(more.begin(),more.end(),std::back_inserter(unreadData));          // Only signal the thread if you have the lock         // Otherwise race conditions happen.         pthread_cond_signal(&cond);          // destructor releases the lock and thus allows read thread to continue.     } };   int main() {     Consumer    c; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to implement a post build event that performs the following actions
I would like to implement something similar to a c# delegate method in PHP.
I would like to implement a data access object pattern in C++, but preferably
I would like to implement a command line interface for a Java application. This
I would like to implement an interactive evolutionary algorithm for generating music (probably just
I would like to implement the method User.calculate_hashed_password . I'm trying to use the
I'm trying to improve performance under high load and would like to implement opcode
I am developing an application for video capture and I would like to implement
I've been working with the SpiderMonkey C API and would like to implement a
I would like to implement the Ramer–Douglas–Peucker_algorithm in C++. The pseudo code looks like

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.