c++ - 什么是 c# AutoResetEvent/ManualResetEvent 的 c++ 可移植模拟?

标签 c++ boost

在 c# 中,我喜欢非常方便的 AutoResetEvent ManualResetEventWaitHandle.WaitAll。 我应该在 c++ 中使用什么(可以使用 boost)来具有相同的功能?我需要可移植的代码,以便我可以在 Windows 和 Linux 上运行它。

最佳答案

我没有完整阅读链接。我猜你可以使用 futures/promises 来实现它们。

promises用于设置事件值,future等待值。

为了获得自动/手动重置,您需要使用智能指针进行额外的间接寻址,以便重新分配 promise/future。

接下来是思路:

template <typename T>
class AutoResetEvent 
{
  struct Impl {
    promise<T> p;
    future<T> f;
    Impl(): p(), f(p) {}
  };
  scoped_ptr<Impl> ptr;
public:
  AutoResetEvent() : ptr(new Impl()) {}
  set_value(T const v) {
    ptr->p.set_value(v);
  }
  T get() {
     T res = ptr->f.get();
     ptr.reset(new Impl());
  }
  void wait() {
     ptr->f.wait();
     ptr.reset(new Impl());
  }
};

您需要做更多的工作才能使它们可移动。

等待多个 futures 只是迭代您要等待的事件的容器。

关于c++ - 什么是 c# AutoResetEvent/ManualResetEvent 的 c++ 可移植模拟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13559993/

相关文章:

c++ - 如何确定boost的静态库路径?

C++ 使用 boost::ptr_vector 泄漏内存

C++清除队列和线程安全

c++ - 尾数较高的fp怎么能代表较小的数呢?

c++ - 如何从Qt5中的QVariant获取QString形式的数据?

c++ - 使用 boost 信号而不是 qt

c++ - Boost.Graph 和 Graphviz 嵌套子图

c++ - 如果程序的一部分表现出未定义的行为,它会影响程序的其余部分吗?

c++ - boost 程序选项计算标志的出现次数

c++ - boost 相当于memcpy?