什么是 C# AutoResetEvent/ManualResetEvent 的 C++ 可移植模拟

what is c++ portable analog of c# AutoResetEvent/ManualResetEvent?

本文关键字:C++ 可移植 模拟 ManualResetEvent AutoResetEvent 什么      更新时间:2023-10-16

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

我还没有完全阅读链接。我想你可以使用期货/承诺来实现它们。

承诺是用来设置事件值的,将来等待值。

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

接下来是这个想法:

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());
  }
};

您将需要更多一点才能使它们可移动。

等待

几个期货只是在你想要等待的事件的容器上进行迭代。