- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
考虑一个原子读-修改-写操作,例如 x.exchange(..., std::memory_order_acq_rel)
.出于对其他对象的加载和存储进行排序的目的,这是否被视为:
x
会同时观察它们还是两者都不观察?std::atomic<int> x, y;
void thread_A() {
x.exchange(1, std::memory_order_acq_rel);
y.store(1, std::memory_order_relaxed);
}
void thread_B() {
// These two loads cannot be reordered
int yy = y.load(std::memory_order_acquire);
int xx = x.load(std::memory_order_acquire);
std::cout << xx << ", " << yy << std::endl;
}
thread_B
可以吗?输出
0, 1
?
x.exchange()
被
x.store(1, std::memory_order_release);
取代然后
thread_B
肯定能输出
0, 1
.是否应该在
exchange()
中添加额外的隐式负载排除这个?
0, 1
是禁止的:
A read-modify-write operation with this memory order is both an acquire operation and a release operation. No memory reads or writes in the current thread can be reordered before or after this store.
thread_A
本质上
ldaxr [x]
stlxr #1, [x]
str #1, [y]
(
See on godbolt. ) 根据我对 ARM64 语义的理解,以及一些测试(负载为
y
而不是存储),我认为
str [y]
可以在
stlxr [x]
之前变得可见(当然不是在
ldaxr
之前)。这将使
thread_B
成为可能观察
0, 1
.所以如果 #1 是真的,那么 gcc 和 clang 似乎都是错误的,我不敢相信。
memory_order_acq_rel
与
seq_cst
不会改变此分析的任何内容,因为它仅添加了与其他
seq_cst
相关的语义。操作,我们这里没有。
0, 1
可以观察到。我仍然希望得到确认,以及检查 cppreference 引用是否实际上是错误的或者我是否误解了它。
最佳答案
不是语言标准层面的答案,而是一些证据表明,在实践中,答案可以是“两个”。正如我在问题中猜测的那样,即使 RMW 是 seq_cst
,也可能发生这种情况。 .
我无法像原始问题一样观察到存储被重新排序,但这里有一个示例显示了原子 seq_cst
的存储RMW 被重新排序为以下 relaxed
加载。
下面的程序是根据 What's are practical example where acquire release memory order differs from sequential consistency? 中 LWimsey 的示例改编的 Peterson 算法的实现。 .正如那里所解释的,该算法的正确版本涉及
me.store(true, std::memory_order_seq_cst);
if (other.load(std::memory_order_seq_cst) == false)
// lock taken
在存储之后负载变得可见是必不可少的。
me.exchange(true, std::memory_order_seq_cst);
if (other.load(std::memory_order_relaxed) == false) {
// Ensure critical section doesn't start until we know we have the lock
std::atomic_thread_fence(std::memory_order_seq_cst);
// lock taken
}
理论上,由于交换操作已获得语义,因此在交换完成后,尤其是在
true
存储后,负载必须变为可见。至
me
已经变得可见。
exchange
确实包含一个获取加载和一个释放存储,以及
other.load
可能会在发布商店之前可见。 (虽然不是在
exchange
的获取加载之前,但这在这里无关紧要。)
mov w11, #1
retry:
ldaxrb wzr, [me]
stlxrb w12, w11, [me]
cbnz w12, retry
ldrb w11, [other]
见
https://godbolt.org/z/fhjjn7 ,汇编输出的第 116-120 行。 (gcc 是相同的,但隐藏在库函数中。)根据 ARM64 内存排序语义,release-store
stlxrb
可以通过以下加载和存储重新排序。它是排他性的这一事实并没有改变这一点。
dc civac
驱逐该行来确保这一点。 .我们还需要放置两个标志
me
和
other
在单独的缓存行上。否则,据我所知,即使线程 A 在存储之前进行加载,线程 B 也必须等到 A 的存储完成后才开始其 RMW,尤其是在 A 的存储可见之前不会进行加载。
-O2
构建.我怀疑如果为 ARMv8.2 或更高版本构建,它将无法工作,其中
swpalb
可用。
// Based on https://stackoverflow.com/a/41859912/634919 by LWimsey
#include <thread>
#include <atomic>
#include <cassert>
// size that's at least as big as a cache line
constexpr size_t cache_line_size = 256;
static void take_lock(std::atomic<bool> &me, std::atomic<bool> &other) {
alignas(cache_line_size) bool uncached_true = true;
for (;;) {
// Evict uncached_true from cache.
asm volatile("dc civac, %0" : : "r" (&uncached_true) : "memory");
// So the release store to `me` may be delayed while
// `uncached_true` is loaded. This should give the machine
// time to proceed with the load of `other`, which is not
// forbidden by the release semantics of the store to `me`.
me.exchange(uncached_true, std::memory_order_seq_cst);
if (other.load(std::memory_order_relaxed) == false) {
// taken!
std::atomic_thread_fence(std::memory_order_seq_cst);
return;
}
// start over
me.store(false, std::memory_order_seq_cst);
}
}
static void drop_lock(std::atomic<bool> &me) {
me.store(false, std::memory_order_seq_cst);
}
alignas(cache_line_size) std::atomic<int> counter{0};
static void critical_section(void) {
// We should be the only thread inside here.
int tmp = counter.fetch_add(1, std::memory_order_seq_cst);
assert(tmp == 0);
// Delay to give the other thread a chance to try the lock
for (int i = 0; i < 100; i++)
asm volatile("");
tmp = counter.fetch_sub(1, std::memory_order_seq_cst);
assert(tmp == 1);
}
static void busy(std::atomic<bool> *me, std::atomic<bool> *other)
{
for (;;) {
take_lock(*me, *other);
std::atomic_thread_fence(std::memory_order_seq_cst); // paranoia
critical_section();
std::atomic_thread_fence(std::memory_order_seq_cst); // paranoia
drop_lock(*me);
}
}
// The two flags need to be on separate cache lines.
alignas(cache_line_size) std::atomic<bool> flag1{false}, flag2{false};
int main()
{
std::thread t1(busy, &flag1, &flag2);
std::thread t2(busy, &flag2, &flag1);
t1.join(); // will never happen
t2.join();
return 0;
}
关于c++ - 出于排序的目的,原子读-修改-写是一种操作还是两种操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65568185/
我正致力于通过 OAuth 合并外部 API,但对 expires_in 属性的用途有点迷惑。通过阅读,应该对 api token 的使用进行防御性编码,因为您应该预料到 token 在任何时候都可能
有人可以概述或总结一下 Spring 框架上下文中 bean 的用途吗? 我了解标准的 Java bean(没有 arg 构造函数、getter/setter,通常是序列化的),但 Spring be
使用 OpenGL 4.1 和 ARB_separate_shader_objects,我们能够在着色器程序中存储着色管道的不同阶段。众所周知,要使用这些,我们需要将它们附加到程序管道对象,然后绑定(
正如我从文档中了解到的那样,“MoveIteratorFactory”的目的是生成每一步都需要执行的 Action 。 “getSize”方法的移动子集有多大? “createOriginalMove
请解释 CMakeLists.txt 中这一行的目的是什么: 包括(InstallRequiredSystemLibraries) 我在 CMake 示例中看到这一行,但找不到好的解释,为什么我需要它
这里是新手。我仍在尝试理解在多个布局中运行单个进程或目的的概念。 例如,我想在我的申请中添加“提交后”功能。有一个包含标题、内容等文本框的主布局,以及一个链接到另一个布局以选择类别的按钮。我的问题是,
我在看 Box Oauth2.0 View Controller : https://github.com/box/box-ios-sdk-v2/blob/master/BoxSDK/OAuth2/B
我编写了一个将字符串复制到系统剪贴板的 Java 应用程序。构造函数使用 Clipboard.setContents(Transferable contents, ClipboardOwner own
阅读此文后:http://sourcemaking.com/design_patterns/command 我还是不太明白为什么我们需要这个。 最佳答案 想法是,如果命令被封装为对象,那么这些命令可以
我知道 c++ 中的模板是做什么的,但是今天我看到了一些奇怪的代码: template <> void swap(foo &a, foo &b) { a.name = b.name; a.
我不太明白 C# Collections 中 IEnumerator 的用途是什么。它的用途是什么,为什么要使用它? 我试着在线查看 http://msdn.microsoft.com/en-us/l
不幸的是,我今天做了一些代码考古(同时重构了一些旧的危险代码)并发现了这样的小化石: # line 7 "foo.y" 能在里面找到如此古老的宝藏,我完全惊呆了。我在 C 编程的网站上阅读了它。然而,
您能否澄清一下此注释的实际用途? - 如果我们没有使用数据库中的 SQL 表定义定义相应的约束,会发生什么情况。当我们尝试插入时,hibernate 会检查唯一性吗?或者这就是DB的目的吗?如果 hi
我在视频教程中看到过这段代码: const navToggle = ["Menu"].join(""); $(".site-header").prepend(navToggle); 我明白它的基本作用
我想知道这个成员函数的 scroll_to(TextBuffer::iterator& iter, double within_margin = 0)参数 within_margin。 API 是这样
我想知道是否可以将子目录提交到目录例如,假设您有 site.com/directory 可以将子目录提交到目录。我即将开始为希望她的网站在搜索引擎中排名靠前的客户进行一些搜索引擎优化。我知道实现此目的
STL 迭代器的用途是什么?为什么程序员要创造这个概念? 最佳答案 迭代器允许您将算法与容器分开。只要您有开始和结束迭代器,并且知道迭代器的功能(随机访问等),您就可以在迭代器指定的范围内进行操作。例
NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&respons
我正在编写代码,使用通用的 linux i2c 驱动程序 linux/i2c-dev.h 实现一个简单的 i2c 读/写功能 我对 ioctl 感到困惑:I2C_SLAVE 内核文档说明如下: You
在尝试克隆可变集合时,我最初的方法是对 mutable.Cloneable 特征使用 clone() 方法。但是,这取决于创建引用副本的 java.Object.clone 实现,而不是深拷贝。通过测
我是一名优秀的程序员,十分优秀!