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

多線程程序卡在優化模式但在 -O0 中正常運行

Multithreading program stuck in optimized mode but runs normally in -O0(多線程程序卡在優化模式但在 -O0 中正常運行)
本文介紹了多線程程序卡在優化模式但在 -O0 中正常運行的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我寫了一個簡單的多線程程序如下:

I wrote a simple multithreading programs as follows:

static bool finished = false;

int func()
{
    size_t i = 0;
    while (!finished)
        ++i;
    return i;
}

int main()
{
    auto result=std::async(std::launch::async, func);
    std::this_thread::sleep_for(std::chrono::seconds(1));
    finished=true;
    std::cout<<"result ="<<result.get();
    std::cout<<"
main thread id="<<std::this_thread::get_id()<<std::endl;
}

它在Visual Studio-O0中在gcc中的調試模式下正常運行,并在1<后打印結果/code> 秒.但是在發布模式或-O1 -O2 -O3下它卡住了并且不打印任何東西.

It behaves normally in debug mode in Visual studio or -O0 in gcc and print out the result after 1 seconds. But it stuck and does not print anything in Release mode or -O1 -O2 -O3.

推薦答案

兩個線程,訪問一個非原子的、非保護的變量是 UB 這涉及finished.您可以制作 std::atomic 類型的 finished 來解決這個問題.

Two threads, accessing a non-atomic, non-guarded variable are U.B. This concerns finished. You could make finished of type std::atomic<bool> to fix this.

我的修復:

#include <iostream>
#include <future>
#include <atomic>

static std::atomic<bool> finished = false;

int func()
{
    size_t i = 0;
    while (!finished)
        ++i;
    return i;
}

int main()
{
    auto result=std::async(std::launch::async, func);
    std::this_thread::sleep_for(std::chrono::seconds(1));
    finished=true;
    std::cout<<"result ="<<result.get();
    std::cout<<"
main thread id="<<std::this_thread::get_id()<<std::endl;
}

輸出:

result =1023045342
main thread id=140147660588864

coliru 現場演示

有人可能會認為'這是一個 bool –大概有一點.這怎么可能是非原子的?(當我自己開始使用多線程時,我就這樣做了.)

Somebody may think 'It's a bool – probably one bit. How can this be non-atomic?' (I did when I started with multi-threading myself.)

但請注意,std::atomic 提供給您的不只是缺乏撕裂.它還使來自多個線程的并發讀+寫訪問得到明確定義,阻止編譯器假設重新讀取變量將始終看到相同的值.

But note that lack-of-tearing is not the only thing that std::atomic gives you. It also makes concurrent read+write access from multiple threads well-defined, stopping the compiler from assuming that re-reading the variable will always see the same value.

使 bool 不受保護、非原子會導致其他問題:

Making a bool unguarded, non-atomic can cause additional issues:

  • 編譯器可能會決定將變量優化為一個寄存器,甚至將 CSE 多次訪問優化為一個,并從循環中提升負載.
  • 可能會為 CPU 內核緩存該變量.(在現實生活中,CPU 具有一致的緩存.這不是一個真正的問題,但 C++ 標準足夠寬松,可以涵蓋非連貫共享內存上的假設 C++ 實現,其中 atomicmemory_order_relaxed 存儲/加載將工作,但 volatile 不會.為此使用 volatile 將是 UB,即使它在實際 C++ 實現中實際工作.)
  • The compiler might decide to optimize variable into a register or even CSE multiple accesses into one and hoist a load out of a loop.
  • The variable might be cached for a CPU core. (In real life, CPUs have coherent caches. This is not a real problem, but the C++ standard is loose enough to cover hypothetical C++ implementations on non-coherent shared memory where atomic<bool> with memory_order_relaxed store/load would work, but where volatile wouldn't. Using volatile for this would be UB, even though it works in practice on real C++ implementations.)

為了防止這種情況發生,必須明確告知編譯器不要這樣做.

To prevent this to happen, the compiler must be told explicitly not to do.

關于 volatile 與這個問題的潛在關系的不斷發展的討論讓我有點驚訝.因此,我想花掉我的兩分錢:

I'm a little bit surprised about the evolving discussion concerning the potential relation of volatile to this issue. Thus, I'd like to spent my two cents:

  • volatile 對線程有用
  • 誰害怕糟糕的優化編譯器?.

這篇關于多線程程序卡在優化模式但在 -O0 中正常運行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

What is the fastest way to transpose a matrix in C++?(在 C++ 中轉置矩陣的最快方法是什么?)
Sorting zipped (locked) containers in C++ using boost or the STL(使用 boost 或 STL 在 C++ 中對壓縮(鎖定)容器進行排序)
Rotating a point about another point (2D)(圍繞另一個點旋轉一個點 (2D))
Image Processing: Algorithm Improvement for #39;Coca-Cola Can#39; Recognition(圖像處理:Coca-Cola Can 識別的算法改進)
How do I construct an ISO 8601 datetime in C++?(如何在 C++ 中構建 ISO 8601 日期時間?)
Sort list using STL sort function(使用 STL 排序功能對列表進行排序)
主站蜘蛛池模板: www.少妇| 视频爱爱免费视频爱爱太爽 | 国产精品久久久久久久久久久久久 | 成人在线a | 亚洲国产成人在线 | 成年人午夜视频 | 黑人巨大精品欧美一区二区 | 视频爱爱免费视频爱爱太爽 | 欧美成人极品 | 欧美黄色片网站 | 黄色大片在线免费观看 | 欧美午夜精品 | 欧美精品福利 | 久久精品久久久久久久 | 国产黄色免费视频 | 国产一区二区网站 | 久久精品三级 | 国产精品久久视频 | 黄色一级片黄色一级片 | 欧美日韩一二三区 | 日韩在线观看中文字幕 | 国产永久视频 | 超碰777| 亚洲一区自拍 | 国产欧美综合一区二区三区 | 成人午夜网 | 亚洲欧美国产毛片在线 | 久久久久久久久久久国产 | 久久激情视频 | 综合激情网| 99久久九九| 东北少妇bbbb搡bbb搡 | 日本在线视频一区二区 | 青青草在线免费视频 | 欧美成人a | 久久久久亚洲精品 | 草草在线观看 | 日韩精品欧美 | a视频在线免费观看 | 婷婷视频在线 | 国产精品免费在线播放 |