- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
AFAIK C++ 原子(<atomic>
)系列提供 3 个好处:
我不确定第三个项目符号,因此请看下面的示例。
#include <atomic>
std::atomic_bool a_flag = ATOMIC_VAR_INIT(false);
struct Data {
int x;
long long y;
char const* z;
} data;
void thread0()
{
// due to "release" the data will be written to memory
// exactly in the following order: x -> y -> z
data.x = 1;
data.y = 100;
data.z = "foo";
// there can be an arbitrary delay between the write
// to any of the members and it's visibility in other
// threads (which don't synchronize explicitly)
// atomic_bool guarantees that the write to the "a_flag"
// will be clean, thus no other thread will ever read some
// strange mixture of 4bit + 4bits
a_flag.store(true, std::memory_order_release);
}
void thread1()
{
while (a_flag.load(std::memory_order_acquire) == false) {};
// "acquire" on a "released" atomic guarantees that all the writes from
// thread0 (thus data members modification) will be visible here
}
void thread2()
{
while (data.y != 100) {};
// not "acquiring" the "a_flag" doesn't guarantee that will see all the
// memory writes, but when I see the z == 100 I know I can assume that
// prior writes have been done due to "release ordering" => assert(x == 1)
}
int main()
{
thread0(); // concurrently
thread1(); // concurrently
thread2(); // concurrently
// join
return 0;
}
首先,请在代码中验证我的假设(尤其是 thread2
)。
其次,我的问题是:
如何a_flag
写入传播到其他核心?
std::atomic
同步 a_flag
在与其他核心缓存(使用 MESI 或其他任何东西)的写入器缓存中,或者传播是自动的?
假设在特定机器上对标志的写入是原子的(想想 x86 上的 int_32)并且我们没有任何私有(private)内存可以同步(我们只有一个标志)我们是否需要使用原子?
考虑到最流行的 CPU 架构(x86、x64、ARM v.whatever、IA-64),跨核可见性(我现在不考虑重新排序)是自动的(但可能会延迟),或者您需要发出特定命令来传播任何数据?
最佳答案
核心本身并不重要。问题是“所有核心如何最终看到相同的内存更新”,这是您的硬件为您做的事情(例如缓存一致性协议(protocol))。内存只有一 block ,所以主要关注的是缓存,这是硬件的私有(private)关注点。
这个问题似乎不清楚。重要的是由 a_flag
的加载和存储形成的获取-释放对,它是一个同步点并导致 thread0
和 thread1
以特定顺序出现(即 thread0
中的所有内容在存储之前 happens-before thread1
中循环之后的所有内容)。
是的,否则你不会有同步点。
您不需要 C++ 中的任何“命令”。 C++ 甚至不知道它运行在任何特定类型的 CPU 上这一事实。凭借足够的想象力,您或许可以在魔方上运行 C++ 程序。 C++ 编译器 选择必要的指令来实现 C++ 内存模型所描述的同步行为,并且在 x86 上涉及发出指令锁前缀和内存栅栏,并且不会过多地重新排序指令。由于 x86 具有强有序内存模型,因此与没有原子性的天真、不正确的代码相比,上述代码应该产生最少的额外代码。
在代码中包含您的 thread2
会使整个程序出现未定义的行为。
只是为了好玩,并且为了表明弄清楚自己正在发生的事情是有启发性的,我将代码编译成三种变体。 (我添加了一个 glbbal int x
并在 thread1
中添加了 x = data.y;
)。
获取/释放:(您的代码)
thread0:
mov DWORD PTR data, 1
mov DWORD PTR data+4, 100
mov DWORD PTR data+8, 0
mov DWORD PTR data+12, OFFSET FLAT:.LC0
mov BYTE PTR a_flag, 1
ret
thread1:
.L14:
movzx eax, BYTE PTR a_flag
test al, al
je .L14
mov eax, DWORD PTR data+4
mov DWORD PTR x, eax
ret
顺序一致:(去除显式排序)
thread0:
mov eax, 1
mov DWORD PTR data, 1
mov DWORD PTR data+4, 100
mov DWORD PTR data+8, 0
mov DWORD PTR data+12, OFFSET FLAT:.LC0
xchg al, BYTE PTR a_flag
ret
thread1:
.L14:
movzx eax, BYTE PTR a_flag
test al, al
je .L14
mov eax, DWORD PTR data+4
mov DWORD PTR x, eax
ret
“朴素”:(仅使用 bool
)
thread0:
mov DWORD PTR data, 1
mov DWORD PTR data+4, 100
mov DWORD PTR data+8, 0
mov DWORD PTR data+12, OFFSET FLAT:.LC0
mov BYTE PTR a_flag, 1
ret
thread1:
cmp BYTE PTR a_flag, 0
jne .L3
.L4:
jmp .L4
.L3:
mov eax, DWORD PTR data+4
mov DWORD PTR x, eax
ret
如您所见,两者没有太大区别。 “不正确”版本实际上看起来大部分是正确的,除了缺少加载(它使用 cmp
和内存操作数)。顺序一致的版本将其昂贵隐藏在 xcgh
指令中,该指令具有隐式锁定前缀并且似乎不需要任何显式栅栏。
关于C++ 原子和跨线程可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19421192/
我在 Mac OsX 10.11 上使用 Xcode 7.0.1 (7A1001) 我使用 carthage 0.9.2 通过以下购物车文件下载reactivecocoa github“Reactiv
我正在将一个对象从属性“模型”(我从 Laravel 中的 Blade 属性模型中获得)分配给数据属性模型。后来数据属性模型发生变化,因为它绑定(bind)到表单输入字段。但 Prop “模型”也发生
当我更新数组内对象的属性然后作为组件的 Prop 传递时,在 svelte 中触发 react 性的正确方法是什么? let items = [{ id: 1, name: 'first'
我是 DRY principle 的坚定拥护者: Every piece of knowledge must have a single, unambiguous, authoritative rep
我正在实现一个需要以下功能的线程: 及时响应终止请求 推送消息 在等待消息时保持对 SendMessage 请求的响应 我对消息泵的初始实现使用了 GetMessage,如下所示: while not
在我的应用程序中,用户获得了一份已到达她的文档列表,并且可以对每个文档执行操作。 文件是分批提交的,当这种情况发生时,列表会增加。这一切都很好,这是预期的行为,但最好有一个按钮“暂停实时数据”,它会忽
我有一个属性为 的数据对象 displaySubtotal 我可以通过以下方式更新该属性的值: data.displaySubtotal = numPad.valueAsAString(); 我的方法
我需要一个垂直 slider 输入。由于内置的 sliderInput 函数无法做到这一点,因此我选择自己实现。根据this thread可以 (I) 使用 CSS 旋转 sliderInput
我正在从自定义用户权限管理系统迁移到 Alanning:roles v2.0 .我有一个非常基本的结构: 基本用户 用户组,每个用户组都有特定的设置。我将它们存储在一个“组”集合中。 管理群组的用户的
Shiny 中的响应式(Reactive)表达式将更改传播到需要去的地方。我们可以使用 isolate 来抑制一些这种行为。 ,但是我们可以抑制基于我们自己的逻辑表达式传播的更改吗? 我给出的例子是一
是否有(或可能有) react 性 Parsec (或任何其他纯函数式解析器)在 Haskell 中? 简而言之,我想逐个字符地为解析器提供数据,并获得与我提供的足够多的结果一样多的结果。 或者更简单
HTML(JADE) p#result Lorem ipsum is javascript j s lo 1 2 4 this meteor thismeteor. meteor input.sear
我有一个被导入函数更改的对象。 https://svelte.dev/repl/e934087af1dc4a25a1ee52cf3fd3bbea?version=3.12.1 我想知道如何使我的更改反
我有一个YUV 420半平面格式的图像,其中字节以这种方式存储: [Y1 Y2 ... [U1 V1.... Yk Yk+1...] Uk' Uk'+1] 其中Y平面的大小是UV平面的两倍,并
如何使用 ReactiveCocoa 订阅从 NSMutableDictionary 添加和删除的对象?另外,我想在它发生变化时广播通知。我的猜测是可以使用 RACMulticastConnectio
我正在构建一个带有多个选项卡的应用程序,其中一些选项卡涉及过多的计算,而另一些选项卡的计算速度很快。一个允许用户在 react 性或手动更新之间进行选择的复选框,与“刷新”按钮结合使用,将是理想的选择
我知道您可以在获取集合时使用 reactive: false 关闭 react 性。如何在内容可编辑区域内的集合字段中实现相同的效果?示例: Template.documentPage.events(
我想在 z3 中表示一个哈希函数,比如 SHA(x)。在做了一些研究之后,似乎 z3 不能很好地支持注入(inject)性,所以我不能有像这样的约束(虽然我意识到这并不是严格意义上的碰撞,但作为一种启
我正在解决一个问题,我想在仪表板中将数据显示为图表(通过 perak:c3 )和表格(通过 aslagle:reactive-table )。我的问题是数据是从 MongoDB 中的集合中提取的,它的
我的 ViewModel 中有这个函数,它返回一个信号,但内部 block 不起作用,我尝试添加断点,但它没有中断。这是我的代码。 func executeLoginAPI() -> RACSigna
我是一名优秀的程序员,十分优秀!