小编Jul*_*ian的帖子

在x86-64多核机器上读取和写入C++ Atomic中的int

我读过这个,我的问题很相似但有点不同.

注意,我知道C++ 0x并不能保证这一点,但我特别要求像x86-64这样的多核机器.

假设我们有2个线程(固定到2个物理内核)运行以下代码:

// I know people may delcare volatile useless, but here I do NOT care memory reordering nor synchronization/
// I just want to suppress complier optimization of using register.
volatile int n; 

void thread1() {
    for (;;)
        n = 0xABCD1234;
        // NOTE, I know ++n is not atomic,
        // but I do NOT care here.
        // what I cares is whether n can be 0x00001234, i.e. in the middle of the update from core-1's …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading atomic cpu-cache

3
推荐指数
3
解决办法
857
查看次数

标签 统计

atomic ×1

c++ ×1

cpu-cache ×1

multithreading ×1