Java循环屏障C++版本是什么?
What is C++ version of Java's cyclic barrier?
在Java中,多个线程可以在某个点等待所有其他线程,这样它们就不会在所有其他线程完成第一个块之前启动新的代码块:
CyclicBarrier barrier = new CyclicBarrier(2);
// thread 1
readA();
writeB();
barrier.await();
readB();
writeA();
// thread 2
readA();
writeB();
barrier.await();
readB();
writeA();
是否有精确或容易转换为C++?
同样在 OpenCL 中,还有一个类似的指令:
readA();
writeB();
barrier(CLK_GLOBAL_MEM_FENCE);
readB();
writeA();
所以所有邻居线程都互相等待,但它只是一个受约束的 C 实现。
C++ STL 没有循环屏障。您可以向标准委员会提出一个:)
像Oracle或Microsoft这样的公司可以快速决定将哪些内容添加到其语言库中。对于C++来说,人们必须达成协议,这可能需要一段时间。
256 个线程很多。与所有与性能相关的问题一样,您需要测量代码以做出明智的决定。对于 256 个线程,我会倾向于使用 10 个由第 11 个屏障同步的屏障。你需要测量才能知道这是否真的更好。
看看我受Java启发的循环屏障C++实现。我几年前写的。它基于我在 http://studenti.ing.unipi.it/~s470694/a-cyclic-thread-barrier/找到的其他人的(错误(代码(链接不再有效......代码非常简单(无需归功于我(。当然,这是原样,没有保证。
// Modeled after the java cyclic barrier.
// Allows n threads to synchronize.
// Call Break() and join your threads before this object goes out of scope
#pragma once
#include <mutex>
#include <condition_variable>
class CyclicBarrier
{
public:
explicit CyclicBarrier(unsigned numThreads)
: m_numThreads(numThreads)
, m_counts{ 0, 0 }
, m_index(0)
, m_disabled(false)
{ }
CyclicBarrier(const CyclicBarrier&) = delete;
CyclicBarrier(CyclicBarrier &&) = delete;
CyclicBarrier & operator=(const CyclicBarrier&) = delete;
CyclicBarrier & operator=(CyclicBarrier &&) = delete;
// sync point
void Await()
{
std::unique_lock<std::mutex> lock(m_requestsLock);
if (m_disabled)
return;
unsigned currentIndex = m_index;
++m_counts[currentIndex];
// "spurious wakeup" means this thread could wake up even if no one called m_condition.notify!
if (m_counts[currentIndex] < m_numThreads)
{
while (m_counts[currentIndex] < m_numThreads)
m_condition.wait(lock);
}
else
{
m_index ^= 1; // flip index
m_counts[m_index] = 0;
m_condition.notify_all();
}
}
// Call this to free current sleeping threads and prevent any further awaits.
// After calling this, the object is no longer usable.
void Break()
{
std::unique_lock<std::mutex> lock(m_requestsLock);
m_disabled = true;
m_counts[0] = m_numThreads;
m_counts[1] = m_numThreads;
m_condition.notify_all();
}
private:
std::mutex m_requestsLock;
std::condition_variable m_condition;
const unsigned m_numThreads;
unsigned m_counts[2];
unsigned m_index;
bool m_disabled;
};
-
C++20 现在有 std::barrier。
-
POSIX具有具有以下接口的"pthread_barrier_t":
int pthread_barrier_init(pthread_barrier_t *restrict barrier, const pthread_barrierattr_t *restrict attr, unsigned count); int pthread_barrier_wait(pthread_barrier_t *barrier); int pthread_barrier_destroy(pthread_barrier_t *barrier);
你可以在 boost 库中找到它,它被称为屏障,缺少等待超时选项。
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 我的设备上的 C++ 编译器版本是什么
- CMake 的导出如何知道目标版本是什么?
- 使用共享库版本支持不同 ABI 的最佳实践是什么?
- 安卓中使用的默认C++版本是什么
- 开发C++应用程序,目标服务器有不同的std lib版本,最佳实践是什么
- Java循环屏障C++版本是什么?
- 如果 std::addressof 是 &.什么是 *& 的可读版本?
- 代码中定义c++版本的内容是什么
- 在版本 440 核心中绘制具有纹理的立方体的顶点和片段着色器应该是什么
- 提升的返回类型是什么::apply_visitor(延迟版本)
- AttachConsole的实际最低版本要求是什么
- gtkmm的g_signal_emit或g_signal_emit_by_name版本是什么?
- 最低合理支持的GCC版本是什么
- 以不同方式实现可变参数构造函数的模板类:每个版本的优点和缺点是什么
- 我的 LLVM & clang (OSX) 版本是什么?
- 支持c++ 11所需的libstdc++的最低合理版本是什么?
- 这个java映射代码的c ++版本是什么
- 格式化输入 - scanf 的C++版本是什么?
- 这个CRC32方法的大端兼容版本是什么样子的?