什么是 C# AutoResetEvent/ManualResetEvent 的 C++ 可移植模拟
what is c++ portable analog of c# AutoResetEvent/ManualResetEvent?
在c#中,我喜欢非常方便的AutoResetEvent
ManualResetEvent
和WaitHandle.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());
}
};
您将需要更多一点才能使它们可移动。
等待几个期货只是在你想要等待的事件的容器上进行迭代。
相关文章:
- C++Union/Struct位域的实现和可移植性
- 具有Qt事件循环的可移植通用共享库设置
- 没有执行策略的 std::transform_reduce 是可移植的吗?
- 如何在 c++ 中正确指定 #include 路径以使程序可移植
- 创建异构顶点数据数组的可移植方法
- FlatBuffers/Protobuf 中是否有支持任意 24 位有符号整数定义的可移植二进制序列化架构?
- 静态库可移植性
- 从非类型模板参数声明 constexpr 数组的可移植方法
- C++:Unicode 字符串文字的可移植性
- 如何使Visual Studio 2017 C++项目在计算机之间更具可移植性
- 尝试将 sfml 和 c++ 与 Windows 10 上的可移植 vscode 链接起来
- 在C++中获取命名空间名称的任何可移植技巧
- STR这个实现是否安全且可移植?
- 编写 std::copysign 的可移植 SSE/AVX 版本
- 是可移植的包装结构
- 将参数推送到调用堆栈 (C++) 的可移植方法
- 在为视频游戏实施基本的二进制序列化时,请担心可移植性
- 如何以可移植方式删除名称为 wchar_t 类型的文件C++
- Xcode 8.3.2 中的错误警告:文件的非可移植路径 - 指定的路径与磁盘上的文件名不同
- 格式化说明符以可移植地打印 C++11 中的 std::uint64_t 变量