- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在研究 Google 的灯丝作业系统。目前,我正在研究他们实现的 WorkStealingDequeue。您可以查看完整的源代码here .这个数据结构是基于这个work .在他们的 pop 和 steal 实现中,他们使用 memory_order_seq_cst 作为完整的内存屏障。
template <typename TYPE, size_t COUNT>
TYPE WorkStealingDequeue<TYPE, COUNT>::pop() noexcept {
// mBottom is only written in push(), which cannot be concurrent with pop(),
// however, it is read in steal(), so we need basic atomicity.
// i.e.: bottom = mBottom--;
int32_t bottom = mBottom.fetch_sub(1, std::memory_order_relaxed) - 1;
// we need a full memory barrier here; mBottom must be written and visible to
// other threads before we read mTop.
int32_t top = mTop.load(std::memory_order_seq_cst);
if (top < bottom) {
// Queue isn't empty and it's not the last item, just return it.
return getItemAt(bottom);
}
TYPE item{};
if (top == bottom) {
// We took the last item in the queue
item = getItemAt(bottom);
// Items can be added only in push() which isn't concurrent to us, however we could
// be racing with a steal() -- pretend to steal from ourselves to resolve this
// potential conflict.
if (mTop.compare_exchange_strong(top, top + 1,
std::memory_order_seq_cst,
std::memory_order_relaxed)) {
// success: mTop was equal to top, mTop now equals top+1
// We successfully poped an item, adjust top to make the queue canonically empty.
top++;
} else {
// failure: mTop was not equal to top, which means the item was stolen under our feet.
// top now equals to mTop. Simply discard the item we just poped.
// The queue is now empty.
item = TYPE();
}
}
// no concurrent writes to mBottom possible
mBottom.store(top, std::memory_order_relaxed);
return item;
}
template <typename TYPE, size_t COUNT>
TYPE WorkStealingDequeue<TYPE, COUNT>::steal() noexcept {
do {
// mTop must be read before mBottom
int32_t top = mTop.load(std::memory_order_seq_cst);
// mBottom is written concurrently to the read below in pop() or push(), so
// we need basic atomicity. Also makes sure that writes made in push()
// (prior to mBottom update) are visible.
int32_t bottom = mBottom.load(std::memory_order_acquire);
if (top >= bottom) {
// queue is empty
return TYPE();
}
// The queue isn't empty
TYPE item(getItemAt(top));
if (mTop.compare_exchange_strong(top, top + 1,
std::memory_order_seq_cst,
std::memory_order_relaxed)) {
// success: we stole a job, just return it.
return item;
}
// failure: the item we just tried to steal was pop()'ed under our feet,
// simply discard it; nothing to do.
} while (true);
}
为了实现正确,要求在 pop() 中 mBottom 在 mTop 之前获取,在 steal() 中 mBottom 在 mBottom 之前获取 mTop。如果我们像大多数实现一样认为 memory_order_seq_cst 是一个完整的内存屏障,那么上面的代码是正确的。但据我所知,C++11 并没有将 memory_order_seq_cst 说成是完整的内存屏障。据我所知,为了确保正确的顺序,mBottom fetch_sub 操作必须至少是 std::memory_order_acq_rel。我的分析正确吗?
然后 mTop 上的 memory_order_seq_cst 是否有必要? memory_order_seq_cst 强制 mTop 上的所有操作都在单个总订单 (STO) 上进行。但在这种情况下,唯一参与 STO 的是 mTop。我相信我们已经有了修改顺序保证,它声明每个线程必须就每个变量相对于自身的修改顺序达成一致。 compare_exchange_strong操作中的memory_order_acq_rel是否足够?
最佳答案
此代码在 steal
中存在数据竞争,因此无论内存顺序如何,都是未定义的行为。
没有什么可以阻止窃取线程调用 getItemAt(top)
读取给定索引处的值,同时拥有队列的工作线程调用 push
足够的时间环绕缓冲区并覆盖条目,或调用 pop
足够的时间清空队列然后调用 push
覆盖该条目。
例如mTop
是 0,mBottom
是 1 => 队列有一个元素。
窃取线程读取 mTop
和 mBottom
. top<bottom
, 所以它会调用 getItemAt(top)
并且由于任务切换而被操作系统挂起。
工作线程调用 pop
.上面写着 mBottom
并设置 bottom
到 0。然后读取 top
(0). 0==0
, 所以我们称 getItemAt(bottom)
检索项目。然后递增 mTop
为 1,并设置 mBottom
到 1。
工作线程然后调用 push
并调用 setItemAt(mBottom)
设置下一个元素,现在是元素 1。
工作线程现在重复这个 push
/pop
舞蹈COUNT
次,因此队列永远不会有超过一个元素,但每次递增 mTop
和 mBottom
所以事件元素在缓冲区中移动直到mBottom & MASK
又是0。
工作线程调用push
因此 setItemAt(mBottom)
,它访问元素 0。操作系统恢复窃取线程,该线程也在访问元素 0 => 在不排序的情况下读取和写入同一位置 => 数据竞争和未定义的行为。
只有在 TYPE
时才可以是std::atomic<T>
对于一些 T
.
假设COUNT
足够大以至于在实践中这永远不会发生,那么push
写信给 mBottom
与 memory_order_release
, 和 steal
用 memory_order_acquire
读取.这意味着对相关数据项的写入发生在读取 steal
中的项目之前, 所以阅读该项目是可以的。即使使用 fetch_sub
也是可见的在 pop
使用 memory_order_relaxed
由于称为“发布序列”的概念。
memory_order_seq_cst
的使用关于 mTop
的负载和成功的比较交换强制操作 mTop
成一个单一的全局总秩序。但是,关于负载的评论 mTop
在 pop
错误:使用memory_order_seq_cst
不会阻止 mBottom.fetch_sub
调用被重新排序,因为这是一个 load
来自 mTop
, 和 fetch_sub
调用使用 memory_order_relaxed
. memory_order_seq_cst
在 load
上不对非 memory_order_seq_cst
施加任何排序从同一线程写入其他变量。
我现在不确定这对代码有什么影响。
关于c++ - Google 的 WorkStealingDequeue 使用 memory_order_seq_cst 作为完整的内存屏障。有效吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56574744/
我遇到了一个奇怪的问题。我有这个: $(document).ready(function () {
我正在编写一个程序,它从列表中读取一些 ID,从中找出不同的 URL,然后将图像保存到我的 C: 驱动器中。 如果我在浏览器中导航到图像 URL,它们就会起作用。此外,如果我尝试从不同的服务器获取图像
我编写了一个 REST WCF RIA Silverlight 4.0 兼容服务,我可以从 javascript + jQuery.1.4.2.js + JSON2.js(当然,还可以从 .NET 4
我很确定这个网站实际上还没有得到回答。一劳永逸地,与 32 位有符号整数范围内的数字字符串匹配的最小正则表达式是什么,范围是 -2147483648至 2147483647 . 我必须使用正则表达式进
我有两个data.table;我想从那些与键匹配的元素中随机分配一个元素。我现在这样做的方式相当慢。 让我们具体点;这是一些示例数据: dt1<-data.table(id=sample(letter
我已经安装了 celery 、RabitMQ 和花。我可以浏览到花港。我有以下简单的工作人员,我可以将其附加到 celery 并从 python 程序调用: # -*- coding: utf-8 -
我正在使用 ScalaCheck 在 ScalaTest 中进行一些基于属性的测试。假设我想测试一个函数,f(x: Double): Double仅针对 x >= 0.0 定义的, 并返回 NaN对于
我想检查文件是否具有有效的 IMAGE_DOS_SIGNATURE (MZ) function isMZ(FileName : String) : boolean; var Signature: W
在 Herbert Schildt 的“Java:完整引用,第 9 版”中,有一个让我有点困惑的例子。它的关键点我无法理解可以概括为以下代码: class Test { public stat
我在工作中查看了一些代码,发现了一些我以前没有遇到过的东西: for (; ;) { // Some code here break; } 我们一直调用包含这个的函数,我最近才进去看看它是
在 Herbert Schildt 的“Java:完整引用,第 9 版”中,有一个让我有点困惑的例子。它的关键点我无法理解可以概括为以下代码: class Test { public stat
我试图编写一个函数,获取 2D 点矩阵和概率 p 并以概率 p 更改或交换每个点坐标 所以我问了一个question我试图使用二进制序列作为特定矩阵 swap_matrix=[[0,1],[1,0]]
这个问题在这里已经有了答案: Using / or \\ for folder paths in C# (5 个答案) 关闭 7 年前。 我在某个Class1中有这个功能: public v
PostgreSQL 10.4 我有一张 table : Column | Type ------------------------- id | integer| title
我正在 Postgresql 中编写一个函数,它将返回一些针对特定时区(输入)计算的指标。 示例结果: 主要问题是这只是一个指标。我需要从其他表中获取其他 9 个指标。 对于实现此目标的更简洁的方法有
我需要在 python 中模拟超几何分布(用于不替换采样元素的花哨词)。 设置:有一个装满人口许多弹珠的袋子。弹珠有两种类型,红色和绿色(在以下实现中,弹珠表示为 True 和 False)。从袋子中
我正在使用 MaterializeCSS 框架并动态填充文本输入。我遇到的一个问题是,在我关注该字段之前,valid 和 invalid css 类不会添加到我的字段中。 即使我调用 M.update
是否有重叠 2 个 div 的有效方法。 我有以下内容,但无法让它们重叠。 #top-border{width:100%; height:60px; background:url(image.jpg)
我希望你们中的一位能向我解释为什么编译器要求我在编译单元中重新定义一个静态固定长度数组,尽管我已经在头文件中这样做了。这是一个例子: 我的类.h: #ifndef MYCLASS_H #define
我正在使用旧线程发布试图解决相同问题的新代码。什么是安全 pickle ? this? socks .py from socket import socket from socket import A
我是一名优秀的程序员,十分优秀!