Multithreading 线程安全循环缓冲区

Multithreading 线程安全循环缓冲区,multithreading,c++11,Multithreading,C++11,我想更改我看到的线程安全循环缓冲区, 在条件变量处添加谓词,如下所示 我需要在缓冲区为空的情况下,在长时间后退出等待状态。。。。这是我的循环缓冲区模板快照: //Template of CIRCULAR BUFFER thread-safe template <typename T> class circ_buffer : private boost::noncopyable{ public: typedef boost::mutex::scoped_lock lock;

我想更改我看到的线程安全循环缓冲区, 在条件变量处添加谓词,如下所示

我需要在缓冲区为空的情况下,在长时间后退出等待状态。。。。这是我的循环缓冲区模板快照:

//Template of CIRCULAR BUFFER thread-safe

template <typename T>
class circ_buffer : private boost::noncopyable{
public:
    typedef boost::mutex::scoped_lock lock;

    circ_buffer(){}
    circ_buffer(int n){cb.set_capacity(n);}

 ........

  struct cb_not_empty{
        boost::circular_buffer<T>& circ_buf;

        cb_not_empty(boost::circular_buffer<T>& circular_buffer_):
            circ_buf(circular_buffer_){}

        bool operator ()() const{
            return !circ_buf.empty();
        }
    };

    template<typename Duration>
    bool timed_wait_and_pop(T& popped_valued,Duration const& wait_duration){
        lock lk(monitor);
        if(!buffer_not_empty.timed_wait(lk,wait_duration,cb_not_empty(the_cb)))
            return false;
        popped_valued = the_cb.front();
        the_cb.pop();
        return true;
    }

 .......

private:
    boost::condition            buffer_not_empty;
    boost::mutex                monitor;
    boost::circular_buffer<T>   cb;
    std::atomic<bool>           circ_buf_flag;   //flag to turn ON/OFF circular buffer: make circ_buf OFF to go out

};
//循环缓冲区线程安全模板
模板
类循环缓冲区:私有boost::不可复制{
公众:
typedef boost::mutex::作用域锁定;
循环缓冲区(){}
循环缓冲区(int n){cb.set_容量(n);}
........
结构cb_不是空的{
boost::循环缓冲区和循环缓冲区;
cb_非_空(boost::循环缓冲区和循环缓冲区):
循环缓冲区{}
布尔运算符()()常量{
return!circ_buf.empty();
}
};
模板
bool timed_wait_和_pop(T&popped_值、持续时间常数和等待时间){
锁lk(监视器);
如果(!buffer_not_empty.timed_wait(lk,wait_duration,cb_not_empty,the_cb)))
返回false;
popped_valued=_cb.front();
_cb.pop();
返回true;
}
.......
私人:
boost::条件缓冲区不为空;
互斥监视器;
boost::循环缓冲区cb;
std::atomic circ_buf_flag;//打开/关闭循环缓冲区的标志:使circ_buf关闭以退出
};
我的gnu编译器给我

error: 'the_cb' was not declared in this scope if(!buffer_not_empty.timed_wait(lk,wait_duration,cb_not_empty(the_cb))) ^ 错误:“未在此作用域中声明_cb” 如果(!buffer_not_empty.timed_wait(lk,wait_duration,cb_not_empty,the_cb))) ^
当您谈到线程安全缓冲区时,通常最好说明您必须支持一个读卡器还是多个读卡器(类似地,一个写卡器还是多个写卡器)。要回答您的问题,请阅读错误消息。找到您希望声明该变量的位置。在拼写、下划线和
等问题上要非常小心
所示代码中似乎没有定义\u cb
。为什么你认为应该在错误点定义它?@Casey我会更小心,我不明白-4个空格-format@hyde对我还不明白关键字“the_cb”(…该文章中的_队列)应该是我的cb私有变量。。。