- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
代码如下:
std::vector<bool> a(req_count_);
std::vector<std::future<void>> waits(req_count_);
for (int i = 0; i < req_count_; i++) {
// send into a threadpool implementation
waits[i] = framework::Async([i, &a] {
a[i] = true; // write true
});
}
for (int i = 0; i < req_count_; i++) {
waits[i].wait(); // memory barrier?
}
int last_req_count = req_count_;
req_count_ = 0;
for (int i = 0; i < last_req_count; i++) {
if (!a[i]) { // read false
return false;
}
}
我的问题是 std::future::wait
是否用作内存屏障? std::future::wait
等待函数调用完成,但函数是否发生在 std::future::wait
(例如,由其他线程可见的函数调用引起的状态突变)?
如果 std::future::wait
不作为内存屏障,我们如何实现线程池以便在 future 完成时自动触发内存屏障?
如果您认为我对内存屏障的理解有误,请指正。
最佳答案
[container.requirements.dataraces]/2 Notwithstanding [res.on.data.races], implementations are required to avoid data races when the contents of the contained object in different elements in the same container, excepting
vector<bool>
, are modified concurrently.[container.requirements.dataraces]/3 [ Note: For a
vector<int> x
with a size greater than one,x[1] = 5
and*x.begin() = 10
can be executed concurrently without a data race, butx[0] = 5
and*x.begin() = 10
executed concurrently may result in a data race. As an exception to the general rule, for avector<bool> y
,y[0] = true
may race withy[1] = true
. —end note ]
强调我的。比赛发生在 a[i] = true;
. vector<bool>
不是真正的容器,访问“元素”需要接触相邻元素的位操作。
关于c++ - std::future::wait 是内存屏障吗? (我无法解释这个数据竞赛),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48452611/
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我正在开发一个项目,该项目允许用户作为访客或注册用户加入。 有应用范围user具有当前用户类型的 LiveData 的对象 private val _isGuest = MutableLiveData
我正在创建一个应用程序来帮助我们的团队管理 Twitter 竞赛。到目前为止,我已经成功地与 API 进行交互,并返回了一组我需要的推文。 我正在努力决定处理数据库中推文存储的最佳方式、检查它们的频率
我在处理这段特殊代码时遇到了一些麻烦。它要么存在竞争条件,要么存在 quint8 问题。 quint8 chunk3[CHUNK_SIZE_MULT]; memset(chunk3,0x00, siz
我有一个带有 background.js 的 Chrome 扩展程序,其中收集和存储有关页面的信息, chrome.webRequest.onCompleted.addListener( func
我在我的应用程序中遇到了竞争条件,每当我暂停调试时,所有或除 1 个线程外的所有线程都会卡在 syscall_thread_switch 上。它在模拟器上更频繁地重现,但在 iPad Air 上也是如
我有一个简单的包,用于在程序运行期间记录统计信息,我发现 go run -race 说其中存在竞争条件。查看该程序,我不确定每次读写都受互斥锁保护时如何出现竞争条件。谁能解释一下? package c
我是一名优秀的程序员,十分优秀!