我在 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::原子指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12959393/