Ale*_*xey 32 c++ multithreading boost opencv
boost库中的Circular_buffer不是线程安全的.所以我将boost :: circular_buffer对象包装在一个类中,如下所示.通过使用条件变量,互斥锁和锁获取/释放,实现了线程之间的相互排斥(我认为).这个实现线程安全吗?
#include <boost/thread/condition.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/thread.hpp>
#include <boost/circular_buffer.hpp>
// Thread safe circular buffer
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);}
void send (T imdata) {
lock lk(monitor);
cb.push_back(imdata);
buffer_not_empty.notify_one();
}
T receive() {
lock lk(monitor);
while (cb.empty())
buffer_not_empty.wait(lk);
T imdata = cb.front();
cb.pop_front();
return imdata;
}
void clear() {
lock lk(monitor);
cb.clear();
}
int size() {
lock lk(monitor);
return cb.size();
}
void set_capacity(int capacity) {
lock lk(monitor);
cb.set_capacity(capacity);
}
private:
boost::condition buffer_not_empty;
boost::mutex monitor;
boost::circular_buffer<T> cb;
};
Run Code Online (Sandbox Code Playgroud)
编辑 这是一个模板类,它接受任何类型的对象(不仅仅是cv::Mat对象).
| 归档时间: |
|
| 查看次数: |
26903 次 |
| 最近记录: |