久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

循環緩沖區的線程安全實現

Thread safe implementation of circular buffer(循環緩沖區的線程安全實現)
本文介紹了循環緩沖區的線程安全實現的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

來自 boost 庫的 Circular_buffer 不是線程安全的.所以我將 boost::circular_buffer 對象包裝在一個類中,如下所示.線程之間的互斥是通過使用條件變量、互斥鎖和鎖獲取/釋放來實現的(我認為).這個實現線程安全嗎?

Circular_buffer from boost library is not thread-safe. So I wrapped boost::circular_buffer object in a class as shown below. Mutual exclusion between the threads is achieved (I think) by using conditional variables, a mutex and a lock acquisition/release. Is this implementation thread safe?

#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;
};

Edit 這現在是一個模板類,它接受任何類型的對象(不僅僅是 cv::Mat 對象).

Edit This is now a template class, which accepts an object of any type (not just cv::Mat object).

推薦答案

是.
如果您使用相同的鎖鎖定所有公共方法,它將是線程安全的.

Yes.
If you lock all the public methods with the same lock it will be threadsafe.

您可以考慮使用讀寫鎖,如果您使用它可能會有更好的性能有很多并發讀者.

You could consider using read-write locks, which may have better performance if you have a lot of concurrent readers.

如果你沒有很多讀者,它只會增加開銷,但可能值得檢查選項和測試.

If you don't have a lot of readers, it will just add overhead, but may be worth checking the option and testing.

這篇關于循環緩沖區的線程安全實現的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Assertion failed (size.widthgt;0 amp;amp; size.heightgt;0)(斷言失敗(size.width0 amp;amp; size.height0))
Rotate an image in C++ without using OpenCV functions(在 C++ 中旋轉圖像而不使用 OpenCV 函數)
OpenCV: process every frame(OpenCV:處理每一幀)
Why can#39;t I open avi video in openCV?(為什么我不能在 openCV 中打開 avi 視頻?)
OpenCV unable to set up SVM Parameters(OpenCV 無法設置 SVM 參數)
Convert a single color with cvtColor(使用 cvtColor 轉換單一顏色)
主站蜘蛛池模板: 精品久久久久久久久久久久久久 | 毛片网站在线观看 | 久久鲁视频 | 国产精品1区2区 | 国产日韩欧美精品一区二区三区 | 欧美日韩电影一区二区 | 中文在线一区二区 | 人人干人人看 | 欧美成人在线网站 | 日韩在线不卡 | 精品国产伦一区二区三区观看方式 | 国产精品久久精品 | 精品欧美一区二区三区精品久久 | 日韩在线综合 | 欧美综合国产精品久久丁香 | 欧美日韩一区二区三区不卡视频 | 亚洲一区视频在线 | 久久久久久久久久久成人 | 91欧美精品成人综合在线观看 | 日韩美女一区二区三区在线观看 | 国产成人精品免费 | 国产精品久久精品 | 日本免费一区二区三区四区 | 日韩午夜在线观看 | 欧美视频二区 | av一区二区三区 | 狠狠色综合久久婷婷 | 久久精品欧美一区二区三区不卡 | 国产精品久久久久久久久图文区 | 国产精品揄拍一区二区 | 日韩成人高清在线 | 福利一区二区在线 | 久久日韩精品 | 91视频www.| 国产高清久久 | 麻豆91精品91久久久 | 亚洲精品一区二区二区 | 久久国际精品 | 亚洲一区二区三区四区五区中文 | 国产精品视频一区二区三区 | 免费 视频 1级 |