gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 23:18:33 24 4
gpt4 key购买 nike

在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com