gpt4 book ai didi

c++ - MinGW 4.6.2 std::atomic 指针

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:53:45 25 4
gpt4 key购买 nike

我在 Windows XP 上使用 MinGW 4.6.2,我遇到了 std::atomic 的一些奇怪行为。情况如下:

  • 线程 A 创建一个 std::atomic 变量(使用 T* 作为模板参数)。
  • 线程 B 修改它。
  • 线程 A 等待修改,然后读取变量。

最终结果是线程A读取的值不是线程B设置的值。

如果我删除 std::atomic(即将变量保存为指针),它会按预期工作。更有趣的是,如果我将模板参数设置为 unsigned long 并将指针转换到 T* 或从 T* 转换,它会按预期工作。

我正在使用赋值运算符来设置值并使用加载成员来获取值。

我是否遗漏了带有 T* 作为参数的 std::atomic 应该如何工作,或者这是一种损坏的行为?

编辑

一些代码

    #include <boost/thread.hpp>
#include <atomic>

using namespace std;

void* vptr;
std::atomic<unsigned int> aui;
std::atomic<void*> aptr;

void foo()
{
vptr = (void*) 0x123;
aui = (unsigned int) 0x123;
aptr = (void*) 0x123;
}

int main(int argc, char* argv[])
{
boost::thread threadA;

vptr = nullptr;
aui = 0;
aptr = nullptr;

threadA = boost::thread(foo);
threadA.join();

cout << vptr << " " << (void*)aui.load() << " " << aptr.load();

return 0;
}

输出是:0x123 0x123 0x41d028

最佳答案

我用 MinGW 4.6.1 重现了您的问题(并发现它已在 4.7.0 中修复)。如果您无法移动到已解决问题的较新 MinGW,您应该能够修补 lib/gcc/mingw32/4.6.2/include/c++/bits/atomic_0.h header 对 _ATOMIC_STORE_ 宏的定义如下(假设 4.6.2 中的 header 与 4.6.1 足够相似):

#if 0 /* disable the original broken macro */
#define _ATOMIC_STORE_(__a, __n, __x) \
({typedef __typeof__(_ATOMIC_MEMBER_) __i_type; \
__i_type* __p = &_ATOMIC_MEMBER_; \
__typeof__(__n) __w = (__n); \
__atomic_flag_base* __g = __atomic_flag_for_address(__p); \
__atomic_flag_wait_explicit(__g, __x); \
*__p = __w; \
atomic_flag_clear_explicit(__g, __x); \
__w; })
#else
#define _ATOMIC_STORE_(__a, __n, __x) \
({typedef __typeof__(_ATOMIC_MEMBER_) __i_type; \
__i_type* __pxx = &_ATOMIC_MEMBER_; \
__typeof__(__n) __w = (__n); \
__atomic_flag_base* __g = __atomic_flag_for_address(__pxx); \
__atomic_flag_wait_explicit(__g, __x); \
*__pxx = __w; \
atomic_flag_clear_explicit(__g, __x); \
__w; })
#endif

问题似乎出在名为 __p 的宏中的局部变量,当使用同样名为 __p 的变量调用宏作为宏参数时,这显然会导致混淆__n。当 __n 扩展为 __p 时,扩展的宏访问局部变量,而不是访问调用者“传入”的变量。

关于c++ - MinGW 4.6.2 std::atomic 指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12959393/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com