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 6214717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:55:36+00:00 2026-05-24T06:55:36+00:00

boost::condition_variable cond; boost::mutex mutex; //thread #1 for(;;) { D * d = nullptr; while(

  • 0
boost::condition_variable cond;
boost::mutex mutex;

//thread #1
for(;;)
{
    D * d = nullptr;

    while( cb.pop(d) )  //cb is a circular buffer and manage is own mutex/lock internally
    {
        //...do something with d
    }
    boost::unique_lock<boost::_mutex> lock( mutex );
    cond.wait( mutex );
}

//thread #2
while(1)
{
    getchar();

    for( int i = 0 ; i < 1000 ; ++i )
    {
        cb.push(new D(i));      //one producer so no lock required

        cond.notify_one();     // no lock required here ?
    }
}

I am wondering if it is ok if my data container has his own lock to avoid data race, and on an other hand boost::wait use his lock/mutex mechanism as it specified by boost documentation ?

Otherwise, thread1 is the consummer, in case I have only one thread which “consumme” it seems that the lock required by wait is a little bit superfluous, isn’t it ?

EDIT: I dont’ take care about missing update.When I receive an update I update an object with the received data. I just want the fresher update, not necesarilly all the udpate

  • 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-24T06:55:37+00:00Added an answer on May 24, 2026 at 6:55 am

    You can have as many locks as you want, but you’ll get race conditions
    unless both the pop and the push are protected by the same mutex as the
    wait and the notify (and the the lock is not freed between the
    decision to wait and the actual wait). The standard idiom is:

    //  thread #1
    // ...
    {
        boost::unique_lock<boost::mutex> lock( mutex );
        while ( !cb.pop( d ) )
            cond.wait( mutex );
    }
    // process d
    
    
    //  thread #2
    // ...
    {
        boost::unique_lock<boost::mutex> lock( mutex );
        cb.push( new D(i) );
        cond.notify_one();
    }
    

    Trying to loop on the pop in thread #1 is more complicated, at least
    if you want to free the lock during processing of d. You’d need
    something like:

    boost::unique_lock<boost::mutex> lock( mutex );
    while ( cb.pop( d ) ) {
        lock.unlock();
        //  process d
        lock.lock();
    }
    cond.wait( mutex );
    

    It’s more complicated, and I don’t see what you’d gain from it. Just
    use the usual pattern, which is known to work reliably.

    FWIW: your code is full of race conditions: for starters: the pop
    fails in thread 1, there’s a context switch, thread 2 does the push
    and the notify, then back to thread 1, which does the cond.wait.
    And waits, despite the fact that there’s something in the queue.

    I might add that there is almost never any justification for types like
    circular buffers to manage their own mutex locking. The granularity is
    too low. The exception is if the pop instruction actually waits until
    something is there, i.e. (based on std::deque):

    T* CircularBuffer::push( std::auto_ptr<T> in )
    {
        boost::unique_lock<boost::mutex> l( myMutex );
        myQueue.push_back( in.get() );
        in.release();  // Only after the push_back has succeeded!
        myCondition.notify_all();
    }
    
    std::auto_ptr<T> CircularBuffer::pop()
    {
        boost::unique_lock<boost::mutex> l( myMutex );
        while ( myQueue.empty() ) {
            myCondition.wait();
        }
        std::auto_ptr<T> result( myQueue.front() );
        myQueue.pop_front();
        return result;
    }
    

    (Note the use of auto_ptr in the interface. Once the provider has
    passed the object into the queue, it no longer has a right to access
    it.)

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

Sidebar

Related Questions

Consider the following C++ member function: size_t size() const { boost::lock_guard<boost::mutex> lock(m_mutex); return m_size;
Is it possible to wait on a boost::condition_variable without having to acquire a mutex
I am using boost::thread to process messages in a queue. When a first message
on linux, gcc 4.3, compiling a class with boost::thread implementation and mutexes / condition
I'm trying to code a wrapper over a boost::asio::ip::tcp::socket Something like that : class
I have programmed a boost::thread application, where I might have some race conditions. I
Boost is meant to be the standard non-standard C++ library that every C++ user
Boost is a very large library with many inter-dependencies -- which also takes a
Boost is a great set of libraries and it really boosts productivity. But debugging
// BOOST Includes #include <boost/assign.hpp> // Boost::Assign #include <boost/assign/list_of.hpp> // Boost::Assign::List_Of #include <boost/assign/std/map.hpp> //

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.