Windows 事件的 Boost 等效项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5598890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Boost Equivalent for Windows Events
提问by rossb83
In windows c++ I can create a handle to event
在 windows c++ 中,我可以创建一个事件句柄
Handle h = CreateEvent(...)
处理 h = CreateEvent(...)
I can then set and reset that event
然后我可以设置和重置该事件
SetEvent(...) and ResetEvent(...)
SetEvent(...) 和 ResetEvent(...)
Finally, I can OpenEvents using the command OpenEvent(...)
最后,我可以使用命令 OpenEvent(...)
Is there a boost equivelent for events?
事件是否有提升等价物?
采纳答案by rossb83
I think you need to use boost::mutex
, boost::unique_lock
, boost::condition_variable
and possibly bool
in order to imitate Events.
我认为您需要使用boost::mutex
, boost::unique_lock
,boost::condition_variable
并且可能bool
是为了模仿事件。
You actually might need sort of WaitForSingleObject
in order to wait for an event. Might be like this:
您实际上可能需要某种方式WaitForSingleObject
来等待事件。可能是这样的:
void wait_for_user_input()
{
boost::unique_lock<boost::mutex> lock(mut);
while(!data_ready)
{
cond.wait(lock);
}
process_user_input(); // it might be not necessary to hold mutex locked here!!!
// if so just add curly braces like this:
// void wait_for_user_input()
// {
// {
// boost::unique_lock<boost::mutex> lock(mut);
// while(!data_ready) { cond.wait(lock); }
// }
// process_user_input();
// }
}
回答by csj
The threadsafe Boost Signals2library might be of use to you. The original Signals library was not thread-safe, but implemented a signals/slots framework which isn't too many miles away from the ideas of events. Since Signals2 is threadsafe, you should be able to utilize it for passing events between threads.
线程安全的Boost Signals2库可能对您有用。最初的 Signals 库不是线程安全的,而是实现了一个信号/插槽框架,它与事件的想法相距不远。由于 Signals2 是线程安全的,您应该能够利用它在线程之间传递事件。
回答by zdan
What you want is named_condition variablefrom the boost interprocess library. The main difference from windows events is that you must use them in conjunction with a named_mutex.
你想要的是来自boost 进程间库的named_condition 变量。与 windows 事件的主要区别在于您必须将它们与named_mutex结合使用。
Note, you can use these boost interprocess primitives within a single process. I assume you need these because you are using OpenEvent (which implies sharing of the object by use of a name). If you can avoid using a name, then you can use the non-named variants from the boost threadlibrary.
请注意,您可以在单个进程中使用这些 boost 进程间原语。我假设您需要这些,因为您使用的是 OpenEvent(这意味着通过使用名称共享对象)。如果可以避免使用名称,则可以使用boost 线程库中的未命名变体。