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

C++11 線程安全隊(duì)列

C++11 thread-safe queue(C++11 線程安全隊(duì)列)
本文介紹了C++11 線程安全隊(duì)列的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我正在處理的一個(gè)項(xiàng)目使用多個(gè)線程來(lái)處理一組文件.每個(gè)線程都可以將文件添加到要處理的文件列表中,所以我把(我認(rèn)為是)一個(gè)線程安全隊(duì)列放在一起.相關(guān)部分如下:

A project I'm working on uses multiple threads to do work on a collection of files. Each thread can add files to the list of files to be processed, so I put together (what I thought was) a thread-safe queue. Relevant portions follow:

// qMutex is a std::mutex intended to guard the queue
// populatedNotifier is a std::condition_variable intended to
//                   notify waiting threads of a new item in the queue

void FileQueue::enqueue(std::string&& filename)
{
    std::lock_guard<std::mutex> lock(qMutex);
    q.push(std::move(filename));

    // Notify anyone waiting for additional files that more have arrived
    populatedNotifier.notify_one();
}

std::string FileQueue::dequeue(const std::chrono::milliseconds& timeout)
{
    std::unique_lock<std::mutex> lock(qMutex);
    if (q.empty()) {
        if (populatedNotifier.wait_for(lock, timeout) == std::cv_status::no_timeout) {
            std::string ret = q.front();
            q.pop();
            return ret;
        }
        else {
            return std::string();
        }
    }
    else {
        std::string ret = q.front();
        q.pop();
        return ret;
    }
}

但是,我偶爾會(huì)在 if (...wait_for(lock, ti??meout) == std::cv_status::no_timeout) { } 塊中出現(xiàn)段錯(cuò)誤,并且在 gdb 中檢查表明由于隊(duì)列為空,出現(xiàn)段錯(cuò)誤.這怎么可能?我的理解是 wait_for 只在收到通知時(shí)返回 cv_status::no_timeout,并且這應(yīng)該只在 FileQueue::enqueue 之后發(fā)生剛剛將一個(gè)新項(xiàng)目推送到隊(duì)列中.

However, I am occasionally segfaulting inside the if (...wait_for(lock, timeout) == std::cv_status::no_timeout) { } block, and inspection in gdb indicates that the segfaults are occurring because the queue is empty. How is this possible? It was my understanding that wait_for only returns cv_status::no_timeout when it has been notified, and this should only happen after FileQueue::enqueue has just pushed a new item to the queue.

推薦答案

根據(jù)標(biāo)準(zhǔn) condition_variables 允許虛假喚醒,即使事件沒(méi)有發(fā)生.在虛假喚醒的情況下,它會(huì)返回 cv_status::no_timeout(因?yàn)樗鼏拘讯皇浅瑫r(shí)),即使它沒(méi)有被通知.正確的解決方案當(dāng)然是在繼續(xù)之前檢查喚醒是否真的合法.

According to the standard condition_variables are allowed to wakeup spuriously, even if the event hasn't occured. In case of a spurious wakeup it will return cv_status::no_timeout (since it woke up instead of timing out), even though it hasn't been notified. The correct solution for this is of course to check if the wakeup was actually legit before proceding.

細(xì)節(jié)在標(biāo)準(zhǔn) §30.5.1 [thread.condition.condvar] 中指定:

The details are specified in the standard §30.5.1 [thread.condition.condvar]:

——當(dāng)通過(guò)調(diào)用 notify_one()、調(diào)用 notify_all()、abs_time 指定的絕對(duì)超時(shí) (30.2.4) 或虛假發(fā)出信號(hào)時(shí),函數(shù)將解除阻塞.

—The function will unblock when signaled by a call to notify_one(), a call to notify_all(), expiration of the absolute timeout (30.2.4) speci?ed by abs_time, or spuriously.

...

返回: cv_status::timeout 如果 abs_time 指定的絕對(duì)超時(shí) (30.2.4) 過(guò)期,則為 cv_status::no_timeout.

Returns: cv_status::timeout if the absolute timeout (30.2.4) speci?edby abs_time expired, other-ise cv_status::no_timeout.

這篇關(guān)于C++11 線程安全隊(duì)列的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

What is the fastest way to transpose a matrix in C++?(在 C++ 中轉(zhuǎn)置矩陣的最快方法是什么?)
Sorting zipped (locked) containers in C++ using boost or the STL(使用 boost 或 STL 在 C++ 中對(duì)壓縮(鎖定)容器進(jìn)行排序)
Rotating a point about another point (2D)(圍繞另一個(gè)點(diǎn)旋轉(zhuǎn)一個(gè)點(diǎn) (2D))
Image Processing: Algorithm Improvement for #39;Coca-Cola Can#39; Recognition(圖像處理:Coca-Cola Can 識(shí)別的算法改進(jìn))
How do I construct an ISO 8601 datetime in C++?(如何在 C++ 中構(gòu)建 ISO 8601 日期時(shí)間?)
Sort list using STL sort function(使用 STL 排序功能對(duì)列表進(jìn)行排序)
主站蜘蛛池模板: 国产精品99久久久久久久久久久久 | 国产综合视频 | 天天搞天天搞 | 天堂一区二区三区 | 免费看黄色录像 | 亚洲 欧美 激情 另类 校园 | 黄视频网站在线观看 | 成人免费毛片aaaaaa片 | 精品一区二区三区三区 | 久久九九国产 | 超碰在线观看97 | 亚洲少妇一区 | 免费看黄色aaaaaa 片 | 午夜免费观看视频 | 久色成人 | 国产成人小视频 | 欧美黄色片在线观看 | 亚洲免费视频一区 | 国产aa视频 | 国产精品手机在线观看 | 亚洲第一在线 | 国内精品久久久久久久久 | 国产视频一二区 | 午夜看片 | 青青久久| 91精品国产乱码久久久 | 久操精品视频 | av网站在线免费观看 | 黄网站免费在线观看 | 成人香蕉网 | 欧美一级淫片免费视频黄 | 中国av在线播放 | 日韩成人在线免费观看 | 日本中文字幕在线播放 | 黄色大片网站 | 国产超碰在线观看 | 91国内精品 | 午夜影院在线观看视频 | 亚洲激情文学 | 精品国产一区二区三 | 91av免费在线观看 |