- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
-Thread 1-
y.store (20, memory_order_release);
x.store (10, memory_order_release);
-Thread 2-
if (x.load(memory_order_acquire) == 10) {
assert (y.load(memory_order_acquire) == 20);
y.store (10, memory_order_release)
}
-Thread 3-
if (y.load(memory_order_acquire) == 10) {
assert (x.load(memory_order_acquire) == 10);
}
GCC Atomic Wiki “Overall Summary”段落说,上面的代码assert(x.load(memory_order_acquire))
可以失败。但我不明白为什么?
我的理解是:
最佳答案
这是一个糟糕的例子,但我想它确实说明了松散的原子是多么令人费解。
[介绍.执行]p9:
Every value computation and side effect associated with a full-expression is sequenced before every value computation and side effect associated with the next full-expression to be evaluated.
[atomics.order]p2:
An atomic operation A that performs a release operation on an atomic object M synchronizes with an atomic operation B that performs an acquire operation on M and takes its value from any side effect in the release sequence headed by A.
因此,显示的评估通过 sequenced-before 和 synchronized-with 关系链接在一起:
Thread 1 Thread 2 Thread 3
y.store(20)
|
| s.b.
V s.w.
x.store(10) --------> x.load() == 10
|
| s.b.
V s.w.
y.store(10) --------> y.load() == 10
|
| s.b.
V
x.load() == ?
因此链中的每个评估发生在下一个之前(参见 [intro.races]p9-10)。
[intro.races]p15,
If a value computation A of an atomic object M happens before a value computation B of M, and A takes its value from a side effect X on M, then the value computed by B shall either be the value stored by X or the value stored by a side effect Y on M, where Y follows X in the modification order of M.
这里,A 是线程 2 中取值为 10 的负载,B 是线程 3 中的负载(在断言中)。由于 A 发生在 B 之前,并且 x
没有其他副作用,因此 B 也必须读取 10。
Herb Sutter 有一个更简单的例子 on his blog :
T1: x = 1;
T2: y = 1;
T3: if( x == 1 && y == 0 ) puts("x first");
T4: if( y == 1 && x == 0 ) puts("y first");
您绝对需要顺序一致性来保证最多打印一行。
关于c++ - 为什么 'acquire/release'在c++11中不能保证顺序一致性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50441014/
condition.acquire(threading.Condition()) 是否类似于lock.acquire(threading.Lock)。两者都可以访问锁吗?我可以使用condition.
我正在尝试执行一个非常基本的示例,演示 accumulate() 的使用Drools 的函数,但出现 java.lang.NullPointerException 异常。 代码如下: Metric.j
我是java初学者,我正在尝试信号量。我尝试编写一个具有编写器和读取器的代码,我只尝试使用 acquire() 和 release(): 1) If a writer is writing, then
我的一个程序中有一个奇怪的问题,一个线程获取一个条件,而另一个线程告诉我没有获取该条件。 为了知道线程是否获得了条件,我做了一些调试信息,看起来是的,他做到了。但是另一个线程告诉我条件没有获得。 这是
我的类(class)有一个互斥锁,定义如下: ACE_Mutex m_specsMutex; 当我使用不带参数的 acquire() 方法时,一切正常。但是当我将它与时间值一起使用时(如下所示),它会
我使用 QSharedMemory 和 QSystemSemaphore 来组织多个进程之间的事件交换。 QSharedMemory 存储接收者的数量和事件数据。接收者在 QSystemSemapho
我正在为我的 REST 服务编写一个 API 库。在某些时候,访问 token 将需要更新。我正在尝试实现一种线程安全的方法来执行此操作,以便仅发送一个更新请求,即使多个线程可能想要同时更新它。 这是
我正在我的node.js 项目中使用MySQL 数据库。我用 Knex 创建了一个数据库查询,结果没问题。但是当我再次尝试查询时,出现此错误: Error: Unable to acquire a c
以下锁定机制用于防止 cron 作业并发运行: #!/bin/bash echo "Before critical section" ( flock -e 200 echo "In c
在 Python 3.4.3 上,我无法理解 threading.Lock.acquire() 如何阻塞,直到锁定状态设置为解锁。 threading.Lock 似乎是上面链接的 _dummy_thr
我有一段代码 locked = lock.acquire(False) if locked: break 根据 python 文档: lock.aquire(False):-
-Thread 1- y.store (20, memory_order_release); x.store (10, memory_order_release); -T
我最近在运行测试时遇到了这个错误。我在本地 MongoDB 服务器 (4.0.5) 上试过了,我也在 Mongo Atlas 上试过了,但遇到了同样的问题。 我尝试增加锁定超时,但没有效果。 我不确定
在Windows平台上,TCriticalSection是通过调用Windows API EnterCriticalSection/LeaveCriticalSection来实现的。 Microsof
我为我的数据库使用 mssql ( https://www.npmjs.com/package/mssql ) 模块。通常我使用导致 pg ( https://www.npmjs.com/packag
我在 Python 中有这个示例,它演示了条件变量的使用。 import logging import threading import time logging.basicConfig(level=
我希望我的应用保持 CPU 运行但关闭屏幕以最大限度地减少电力浪费。 关于此主题的先前帖子建议采用以下方法: mPm = (PowerManager) getSystemService(Con
是否可以在 C#.NET 应用程序中请求 Windows 7 PC 上的管理权限? 我希望能够通过 Click Once 部署应用程序,并让用户使用它来执行管理任务(在本例中,它正在为主应用程序编写注
我正在更新 Delphi (Delphi 2009) 代码,它专门使用 TCriticalSection.Acquire/Release 对,而不是 Enter/Release 或 Leave 对。我
我试图理解使用条件变量时丢失唤醒的问题。我相信我已经使用了下面正确的设计模式。消费者: lock the mutex while the condition is not satisfied
我是一名优秀的程序员,十分优秀!