- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我不明白,为什么没有release sequence
会出问题,如果我们在下面的示例中有 2 个线程。我们对原子变量 count
只有 2 个操作. count
如输出所示,按顺序递减。
来自安东尼威廉姆斯的 C++ Concurrency in Action:
I mentioned that you could get a
synchronizes-with relationship
between astore
to an atomic variable and aload
of that atomic variable from another thread, even when there’s a sequence ofread-modify-write
operations between thestore
and theload
, provided all the operations are suitably tagged. If the store is tagged withmemory_order_release
,memory_order_acq_rel
, ormemory_order_seq_cst
, and the load is tagged withmemory_order_consume
,memory_order_acquire
, ormemory_order_seq_cst
, and each operation in the chain loads the value written by the previous operation, then the chain of operations constitutes a release sequence and the initial storesynchronizes-with
(formemory_order_acquire
ormemory_order_seq_cst
) or isdependency-ordered-before
(formemory_order_consume
) the final load. Any atomic read-modify-write operations in the chain can have any memory ordering (evenmemory_order_relaxed
).To see what this means (release sequence) and why it’s important, consider an
atomic<int>
being used as a count of the number of items in a shared queue, as in the following listing.One way to handle things would be to have the thread that’s producingthe data store the items in a shared buffer and then do
count.store(number_of_items, memory_order_release)
#1 to let the other threads know that data is available. The threads consuming the queue items might then docount.fetch_sub(1,memory_ order_acquire)
#2 to claim an item from the queue, prior to actually reading the shared buffer #4. Once the count becomes zero, there are no more items, and the thread must wait #3.
#include <atomic>
#include <thread>
#include <vector>
#include <iostream>
#include <mutex>
std::vector<int> queue_data;
std::atomic<int> count;
std::mutex m;
void process(int i)
{
std::lock_guard<std::mutex> lock(m);
std::cout << "id " << std::this_thread::get_id() << ": " << i << std::endl;
}
void populate_queue()
{
unsigned const number_of_items = 20;
queue_data.clear();
for (unsigned i = 0;i<number_of_items;++i)
{
queue_data.push_back(i);
}
count.store(number_of_items, std::memory_order_release); //#1 The initial store
}
void consume_queue_items()
{
while (true)
{
int item_index;
if ((item_index = count.fetch_sub(1, std::memory_order_acquire)) <= 0) //#2 An RMW operation
{
std::this_thread::sleep_for(std::chrono::milliseconds(500)); //#3
continue;
}
process(queue_data[item_index - 1]); //#4 Reading queue_data is safe
}
}
int main()
{
std::thread a(populate_queue);
std::thread b(consume_queue_items);
std::thread c(consume_queue_items);
a.join();
b.join();
c.join();
}
id 6836: 19
id 6836: 18
id 6836: 17
id 6836: 16
id 6836: 14
id 6836: 13
id 6836: 12
id 6836: 11
id 6836: 10
id 6836: 9
id 6836: 8
id 13740: 15
id 13740: 6
id 13740: 5
id 13740: 4
id 13740: 3
id 13740: 2
id 13740: 1
id 13740: 0
id 6836: 7
If there’s one consumer thread, this is fine; the
fetch_sub()
is a read, withmemory_order_acquire
semantics, and the store hadmemory_order_release
semantics, so the store synchronizes-with the load and the thread can read the item from the buffer.If there are two threads reading, the second
fetch_sub()
will see the value written by the first and not the value written by the store. Without the rule about therelease sequence
, this second thread wouldn’t have ahappens-before relationship
with the first thread, and it wouldn’t be safe to read the shared buffer unless the firstfetch_sub()
also hadmemory_order_release
semantics, which would introduce unnecessary synchronization between the two consumer threads. Without therelease sequence
rule ormemory_order_release
on thefetch_sub
operations, there would be nothing to require that the stores to thequeue_data
were visible to the second consumer, and you would have a data race.
count
的值是
20
?但在我的输出
count
在线程中依次递减。
Thankfully, the first
fetch_sub()
does participate in the release sequence, and so thestore()
synchronizes-with the secondfetch_sub()
. There’s still no synchronizes-with relationship between the two consumer threads. This is shown in figure 5.7. The dotted lines in figure 5.7 show the release sequence, and the solid lines show thehappens-before relationships
最佳答案
What does he mean? That both threads should see the value of count is 20? But in my output count is sequently decremented in threads.
count
是原子的,所以在给定的代码中,两个读取器线程总是会看到不同的值。
release
时存储,然后执行的其他多个线程
acquire
相同位置的负载形成一个释放序列,其中每个后续
acquire
加载与存储线程有先发生的关系(即存储的完成发生在加载之前)。这意味着读取器线程中的加载操作是与写入器线程的同步点,写入器中存储之前的所有内存操作都必须完成并在其相应加载完成时在读取器中可见。
queue
时会发生数据竞争。 (注意:不是
count
,它无论如何都受到原子访问的保护)。理论上,对数据的内存操作发生在
store
之前。在
count
只有在
count
上执行自己的加载操作后,读取器线程编号 2 才能看到它.发布顺序规则确保这不会发生。
关于c++ - "release sequence"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38565650/
给定一个 Sequence of Sequences 类型,如何将其转换为单个扁平化 Sequence 类型?考虑以下 Ceylon 代码: Integer[] range(Integer max)
出于学习目的,我正在尝试使用 F# 以序列形式运行模拟。从一系列随机数开始,如果状态不依赖于先前的状态,map 是生成状态序列的直接方法。我遇到问题的地方是当我尝试做类似的事情时: State(i+1
我正在 DynamoDB 上开发论坛。 有一个帖子表,其中包含线程中的所有帖子。我需要对帖子中的顺序有一个概念,即我需要知道哪个帖子先出现,哪个后出现。 我的服务将在分布式环境中运行。 我不确定使用时
我正在 DynamoDB 上开发论坛。 有一个帖子表,其中包含线程中的所有帖子。我需要对帖子中的顺序有一个概念,即我需要知道哪个帖子先出现,哪个后出现。 我的服务将在分布式环境中运行。 我不确定使用时
在 Z3 中,它支持 String 和 Sequence。但是 Z3py 是否也支持它们,或者我们必须使用 Python 中的字符串或列表?从最新的版本来看,新版本好像确实支持了String和Sequ
我是 Clojure 世界的新手,我遇到了一个问题。我得到了一个 LazySeq,看起来像这样(实际上更长) values = (("Brand1" "0") ("Brand2" "15") ("Br
我正在开发一个用于文本生成的序列到序列模型 ( paper )。我没有在解码器端使用“教师强制”,即 t0 时解码器的输出被馈送到 t1 时解码器的输入。 现在,实际上,解码器(LSTM/GRU)的输
Rust 中的规则是什么,类似于这里描述的规则http://en.cppreference.com/w/cpp/language/eval_order对于 C++? 目前我凭经验发现, 1) 函数的参
我当前的代码: import re from Bio.Seq import Seq def check_promoter(binding_element,promoter_seq): promoter
您好,此代码旨在存储使用 open cv 绘制的矩形的坐标,并将结果编译为单个图像。 import numpy as np import cv2 im = cv2.imread('1.jpg') im
在我的程序中,我有一个正则表达式,它确保输入字符串至少有一个字母和一个数字字符,并且长度在 2 到 10 之间。 Pattern p = Pattern.compile("^(?=.*\\d)(?=.
我正在查看 Google 的免费机器学习速成类(class),并尝试根据他们类(class)的第一部分制作一个预测模型。但是,在输入函数中,有一个字典,我不断收到此错误, in my_input_fn
我想使用 Boost 的 any_range 来处理多个异构数据范围。我的数据范围类型称为 fusion vector ,例如: typedef vector TypeSequence 鉴于这样的类型
我正在使用 SimpleJdbcInsert 作为, SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(dataSource).with
我正在尝试通过从我的数据创建 .phy 文件来创建系统发育树。 我有一个数据框 ndf= ESV trunc 1 esv1 TACGTAGGTG... 2 esv2 TACGGAGGGT... 3 e
这可能真的很简单,但我正处于 Rx 学习曲线的底部。我花了几个小时阅读文章、观看视频和编写代码,但我似乎对一些看起来应该非常简单的事情有心理障碍。 我正在从串行端口收集数据。我已使用 Observab
我正在将一些模块从 v8 迁移到 v10,我有这个模型: class SearchInfoPartnerSeniat(models.TransientModel): _name = "search.i
我尝试添加一个新的“自定义”序列到我的Marten DB中,以获取新用户的用户ID(在注册过程中)。。后来,我能够访问下一个序列值,如下所示:。问题出在上面的代码中:在第一次运行时:将userid_s
我在 rosettacode 遇到了这个代码 my @pascal = [1], { [0, |$_ Z+ |$_, 0] } ... Inf; .say for @pascal[^4]; # ==>
我不明白为什么这个程序有效: my $supply = Supply.interval: 1; react { whenever $supply { put "Got $^a" }
我是一名优秀的程序员,十分优秀!