Search code examples
c++boostmessage-queuecondition-variable

Multiple Condition variable calling each other function


I came across tutorial here Bounding Buffer example. For reference i am also pasting it here.

#include <boost/circular_buffer.hpp>
   #include <boost/thread/mutex.hpp>
   #include <boost/thread/condition.hpp>
   #include <boost/thread/thread.hpp>
   #include <boost/call_traits.hpp>
   #include <boost/progress.hpp>
   #include <boost/bind.hpp>

   template <class T>
   class bounded_buffer {
   public:

      typedef boost::circular_buffer<T> container_type;
      typedef typename container_type::size_type size_type;
      typedef typename container_type::value_type value_type;
      typedef typename boost::call_traits<value_type>::param_type param_type;

      explicit bounded_buffer(size_type capacity) : m_unread(0), m_container(capacity) {}

      void push_front(boost::call_traits<value_type>::param_type item) {
         // param_type represents the "best" way to pass a parameter of type value_type to a method

         boost::mutex::scoped_lock lock(m_mutex);
         m_not_full.wait(lock, boost::bind(&bounded_buffer<value_type>::is_not_full, this));
         m_container.push_front(item);
         ++m_unread;
         lock.unlock();
         m_not_empty.notify_one();
      }

      void pop_back(value_type* pItem) {
         boost::mutex::scoped_lock lock(m_mutex);
         m_not_empty.wait(lock, boost::bind(&bounded_buffer<value_type>::is_not_empty, this));
         *pItem = m_container[--m_unread];
         lock.unlock();
         m_not_full.notify_one();
      }

   private:
      bounded_buffer(const bounded_buffer&);              // Disabled copy constructor
      bounded_buffer& operator = (const bounded_buffer&); // Disabled assign operator

      bool is_not_empty() const { return m_unread > 0; }
      bool is_not_full() const { return m_unread < m_container.capacity(); }

      size_type m_unread;
      container_type m_container;
      boost::mutex m_mutex;
      boost::condition m_not_empty;
      boost::condition m_not_full;
   };

When I try to push the first element(push_front). " push_front()" function reaches condition variable and and goes to wait until it receives a notification once it receives the notification it gonna check function "is_not_full()" and then proceed. But since it's the first element trying to be pushed, it will never receives the notification since the function "pop_back()" will not complete the execution because the buffer is empty.

Am I thinking it correctly ? can i able to ever push element to this piece of code ?

Thanks


Solution

  • When I try to push the first element(push_front). " push_front()" function reaches condition variable and and goes to wait until it receives a notification [...]

    This is false. Read the documentation of this API carefully. What you describe is the behavior of

    template<typename L> void wait(L & lock);
    

    which is number 3 in the list of public member functions. However, the call to wait in your code has two arguments; your code calls

    template<typename L, typename Pr> void wait(L & lock, Pr pred);
    

    which is number 4 in that list. This function is documented as being the same as

    while ( !pred() )
        wait(lock);  // <-- This is where the thread waits for a notification
    

    That is, the predicate is checked first, and only if it is false will the thread wait until a notification is received. As long as an empty buffer is not full, the predicate will be initially true, so execution proceeds without waiting for a notification. (However, if you were to construct a buffer with a capacity of zero, then this does look like it will hang when trying to push the first element into the buffer.)