c++ - C++ 11中的thread_local不一致

标签 c++ multithreading c++11 thread-local

当我将1对象声明为Counter时,为什么输出不一致thread_local

#include <iostream>
#include <thread>
#define MAX 10
    
class Counter {
    int c;
public:
    Counter() : c(0) {}

    void increment() {
        ++c;
    }

    ~Counter() {   
        std::cout << "Thread " << std::this_thread::get_id() << " having counter value " << c << "\n";  
    }
};
    
thread_local Counter c;
    
void doWork() {
    c.increment();
}
    
int main() {
    std::thread t[MAX];
    for ( int i = 0; i < MAX; i++ )
        t[i] = std::thread(doWork);
    for ( int i = 0; i < MAX; i++ )
        t[i].join();    
    return 0;
}
输出 :
Thread 2 having counter value 19469616
Thread 3 having counter value 1
Thread 4 having counter value 19464528
Thread 5 having counter value 19464528
Thread 7 having counter value 1
Thread 6 having counter value 1
Thread 8 having counter value 1
Thread 9 having counter value 1
Thread 10 having counter value 1
Thread 11 having counter value 1

最佳答案

我从探索这一点中学到的唯一有趣的事情是MSVC的运行方式不同于clang/gcc。
这是最新编译器上的MSVC输出:

Thread 34316 having counter value 1
Thread 34312 having counter value 1
Thread 34320 having counter value 1
Thread 34328 having counter value 1
Thread 34324 having counter value 1
Thread 34332 having counter value 1
Thread 34336 having counter value 1
Thread 34340 having counter value 1
Thread 34344 having counter value 1
Thread 34348 having counter value 1
Thread 29300 having counter value 0

c:\dv\TestCpp\out\build\x64-Debug (default)\TestCpp.exe (process 27748) exited with code 0.
注意:“线程29300的计数器值为0”。这是主线程。由于从未在主线程中调用过doWork(),因此计数器值为0。 “c”是全局Counter对象-应该在所有线程(包括主线程)中创建。
但是,当我使用rextester.com在clang和gcc下运行(godbolt似乎不喜欢运行多线程程序,AFAICT)时,我得到:
Thread 140199006193408 having counter value 1
Thread 140198906181376 having counter value 1
Thread 140198806169344 having counter value 1
Thread 140198706157312 having counter value 1
Thread 140199106205440 having counter value 1
Thread 140199206217472 having counter value 1
Thread 140199306229504 having counter value 1
Thread 140199406241536 having counter value 1
Thread 140199506253568 having counter value 1
Thread 140199606265600 having counter value 1
注意:没有主计数器的计数器值为0。“c” Counter对象从未在gcc和clang下的主线程中创建和销毁,但是它是在MSVC下的主线程中创建和销毁的。
我相信您的个人错误是您正在使用的旧版本编译器中存在错误。无论使用哪种最新版本的编译器,我都无法重现您的错误,但是我将向MSVC团队提供一个错误,因为这是Standard C++,并且有一个数字表明无论产生什么结果,它都应该产生相同的结果。编译器,而事实并非如此。
似乎(检查https://en.cppreference.com/w/cpp/language/storage_duration)MSVC可能做错了,因为在主线程中从未显式访问thread_local“Counter c”对象,但是该对象已创建和销毁。
我们会看到他们说的话-我还有一些其他的错误有待解决...
进一步的研究:cpp引用文章似乎表明,由于这是一个“纯全局”,即它不是本地静态线程本地对象,例如:
Foo &
GetFooThreadSingleton()
{
  static thread_local Foo foo;
  return foo;
}
那么他们阅读cppreference文章的方式是:与其他任何全局变量一样,它是一个全局变量,在这种情况下,MSVC可以正确执行此操作。
如果我错了,请有人纠正我。谢谢。
同样,如果没有在gdb内的Ubuntu中本地运行它并进行检查,我无法确定全局实际上不是在gcc/clang下的主线程中创建和销毁的,因为可能由于某些原因我们错过了该输出。我承认这似乎不太可能。我稍后会再尝试,因为这个问题使我有些感兴趣。
我在Ubuntu上 native 运行,并达到了我的期望:全局“Counter c”仅在主线程中访问时才创建和销毁。所以我认为这是目前gcc/clang中的错误。

关于c++ - C++ 11中的thread_local不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66419976/

相关文章:

C++ 类 : Object that creates Thread + pointer to the function = Access violation

C++,当每个工作线程必须执行几个不同的任务时,如何为任务实现线程池

c++ - 接受字符串和整数的可变参数函数,格式化后者并连接所有?

c++ - 验证 Unicode 字符串并在 Unicode 无效时转义 (C/C++)

java - 从 Java 到 C++ 的 JNI 转换

android - 如何请求和删除从线程到服务的 locationManager 更新

c++ - C++11 是否优化了 lambda 中的尾递归调用?

c++ - Clang++ 生成泄漏内存的可执行文件,关于 std::function 和 lambda

c++ - 格式化 C 字符串

c++ - 通过检查其返回值作为 -32767 和 0x8000 来使用 GetAsyncKeyState() 有什么区别?