【发布时间】:2014-02-24 08:59:14
【问题描述】:
在我的代码中,我有以下结构:
父线程
somedatatype thread1_continue, thread2_continue; // Does bool guarantee no data race?
线程 1:
while (thread1_continue) {
// Do some work
}
线程 2:
while (thread2_continue) {
// Do some work
}
所以我想知道哪种数据类型应该是 thread1_continue 或 thread2_continue 以避免数据竞争。如果 pthread 中有任何数据类型或技术可以解决这个问题。
【问题讨论】:
-
std::atomic<bool>使用 C++11。 -
std::atomic<bool>怎么样?
标签: c++ multithreading parallel-processing synchronization pthreads