c++ - 多个条件变量互相调用函数

标签 c++ boost message-queue condition-variable

我遇到了教程 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;
   };

当我尝试推送第一个元素 (push_front) 时。 “push_front()”函数到达条件变量并等待直到收到通知,一旦收到通知,它将检查函数“is_not_full()”,然后继续。但是因为它是第一个试图被压入的元素,它永远不会收到通知,因为函数“pop_back()”不会完成执行,因为缓冲区是空的。

我的想法正确吗?我可以将元素推送到这段代码吗?

谢谢

最佳答案

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 [...]

这是错误的。阅读the documentation仔细研究这个 API。你描述的是行为

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

这是公共(public)成员函数列表中的第 3 个。但是,代码中对 wait 的调用有两个 参数;你的代码调用

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

这是该列表中的第 4 位。此功能被记录为与

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

也就是说,首先检查谓词,只有当它为假时,线程才会等待,直到收到通知。只要空缓冲区未满,谓词最初就会为真,因此无需等待通知即可继续执行。 (但是,如果您要构造一个容量为零的缓冲区,那么在尝试将第一个元素插入缓冲区时,这看起来确实会挂起。)

关于c++ - 多个条件变量互相调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58453984/

相关文章:

c++ - 递归模板不能像预期的那样使用静态变量

c++ - cout 的缓冲区如何工作?

c++ - 使用 boost::program_options 作为类的静态成员

rabbitmq - 消息、队列和交换器有哪些限制?

grails - 由消息队列触发时,Grails服务未保存域对象

c++ - C++ 中两个 vector 之间的元素交换

c++ - 什么是 undefined reference /未解析的外部符号错误以及如何修复它?

c++ - boost::共享_??对于非指针资源

c++ - 使用qi::lexeme解析 '.'链式标识符列表,并防止空间跳过

java - JMS QueueBrowser 卡在 getEnumeration() 上