tow*_*120 8 c++ gcc thread-safety thread-local-storage mingw-w64
这段代码:
#include <iostream>
#include <thread>
#include <mutex>
struct Singl{
Singl(Singl const&) = delete;
Singl(Singl&&) = delete;
inline static thread_local bool alive = true;
Singl(){
std::cout << "Singl() " << std::this_thread::get_id() << std::endl;
}
~Singl(){
std::cout << "~Singl() " << std::this_thread::get_id() << std::endl;
alive = false;
}
};
static auto& singl(){
static thread_local Singl i;
return i;
}
struct URef{
~URef(){
const bool alive = singl().alive;
std::cout << alive << std::endl;
}
};
int main() {
std::thread([](){
singl();
static thread_local URef u;
}).join();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
有以下输出:
Singl() 2
Singl() 2
1
~Singl() 2
~Singl() 2
Run Code Online (Sandbox Code Playgroud)
我用mingw-w64 gcc7.2 posix线程在windows下编译和运行.
Coliru有不同的输出:http://coliru.stacked-crooked.com/a/3da415345ea6c2ee
这是什么,我的工具链/编译器有问题,或者它应该是怎么回事?为什么我在同一个线程上有两个thread_local对象(或构造两次?)?
| 归档时间: |
|
| 查看次数: |
510 次 |
| 最近记录: |