#include <iostream>
#include <unordered_map>
#include <vector>
#include <thread>
using namespace std;
// not POD
struct A {
std::unordered_map<int, int> m_test;
};
struct B{
thread_local static A a;
};
thread_local A B::a = A();
B b;
void func(){
b.a.m_test[0] ;
}
int main() {
vector<thread> Threads;
for (int i = 0; i < 10; i ) {
Threads.push_back(thread(func));
}
for (int i = 0; i < 10; i ) {
Threads[i].join();
}
return 0;
}
代碼片段如上所示。我在 Linux 中構建了相同的代碼:gcc 4.8.5和MacOS:clang13.1.6,結果不同。在 Linux 中,發生錯誤為17703 Floating point exception(core dumped),但在 MacOS 中沒有發生錯誤。
我知道thread_local可以在c 11之后的POD型別中使用,但是這里我使用的是struct中的unordered_map,它的內部記憶體在堆中,而不是在靜態或全域存盤區。所以我想知道這是否是因為不同的編譯器如何實作 C 標準?我該如何解決linux平臺上的這個運行時錯誤?
uj5u.com熱心網友回復:
根據對編譯器資源管理器的測驗,這似乎是 2019 年針對 9 、8.4 和 7.5 版本修復的 GCC 錯誤。代碼應該可以正常作業。沒有什么問題。
大概就是這個bug。
我建議您安裝并使用更新版本的 GCC。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/486658.html
