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

循環(huán)緩沖區(qū)的線程安全實現(xiàn)

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

問題描述

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

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 這現(xiàn)在是一個模板類,它接受任何類型的對象(不僅僅是 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.

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

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.

這篇關(guān)于循環(huán)緩沖區(qū)的線程安全實現(xiàn)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

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++ 中旋轉(zhuǎn)圖像而不使用 OpenCV 函數(shù))
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 無法設(shè)置 SVM 參數(shù))
Convert a single color with cvtColor(使用 cvtColor 轉(zhuǎn)換單一顏色)
主站蜘蛛池模板: 日本韩国欧美中文字幕 | 亚洲色妞 | 蜜臀久久99精品久久久久宅男 | 日日夜夜精品 | 欧美福利在线 | 人人超碰在线 | 婷婷久久五月天 | 福利视频一区二区 | 中国免费av | xxxx性欧美| 中文字幕在线观看亚洲 | 日韩视频免费大全中文字幕 | 亚洲一区欧美一区 | 一级免费片 | 精品在线一区二区 | 久久久www成人免费精品 | 手机成人在线视频 | 福利视频网址导航 | 欧美激情亚洲 | 欧美日韩免费 | 狠狠久 | 91美女视频 | www.桃色| 免费看黄色aaaaaa 片 | 亚洲国产天堂 | 狠狠干av | 伊人久久综合 | 日本特黄特色aaa大片免费 | 国产精品欧美精品 | 三上悠亚一区 | 韩日视频 | 中文字幕在线一区二区三区 | 97caoporn| 99国产在线观看 | 欧美精品日韩 | 中文在线视频 | 色婷五月天 | 国产精品久久久久久久久久久久久久 | 久久国产亚洲 | 少妇在线观看 | 91久久久精品 |