Win32 事件与信号量
基本上我需要替换 Condition Variable 和 SleepConditionVariableCS,因为它只支持 Vista 和 UP。 (对于C++)
有人建议使用Semaphore,我也找到了CreateEvent。
基本上,我需要有一个线程等待 WaitForSingleObject,直到一个或多个其他线程告诉我有事情要做。
我应该在什么情况下使用信号量和获胜事件?
谢谢
Basically I need a replacement for Condition Variable and SleepConditionVariableCS because it only support Vista and UP. (For C++)
Some suggested to use Semaphore, I also found CreateEvent.
Basically, I need to have on thread waiting on WaitForSingleObject, until something one or more others thread tell me there is something to do.
In which context should I use a Semaphore vs an Win Event?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在你的情况下,我会自己使用一个事件。当您希望线程开始运行时,向该事件发出信号。工作完成:)
编辑:信号量和事件之间的区别归结为内部计数。如果有多个ReleaseSemaphore,那么2个WaitForSingleObjects也会被释放。事件本质上是布尔值。如果两个不同的地方同时发出信号事件,那么等待将被释放,并将被设置回未发出信号的状态(取决于您是否有自动或手动重置)。如果您需要同时从多个位置发出信号并且等待线程运行两次,那么此事件行为可能会导致死锁。
In your case I'd use an event myself. Signal the event when you want the thread to get going. Job done :)
Edit: The difference between semaphores and events comes down to the internal count. If there are multiple ReleaseSemaphores then 2 WaitForSingleObjects will also be released. Events are boolean by nature. If 2 different places Signal event simultaneously then the wait will get released and it will get set back to unsignalled (dependent on if you have automatic or manual resetting). If you need it to be signalled from multiple places simultaneously and for the waiting thread to run twice then this event behaviour could lead to a deadlock.
一般情况下,在 Windows 上替换条件变量极其困难且容易出错。要么:
Replacing condition variables on Windows is extremely difficult and error-prone in the general case. Either:
使用 boost::condition_variable< /a> 如果可能的话。我以前走过这条路(请参阅 microsoft.public.win32.programmer.kernel) 和 Win32 Event API 还不够;使用事件有问题。
Use boost::condition_variable if at all possible. I've been down this road before (see msg on microsoft.public.win32.programmer.kernel) and the Win32 Event API does not suffice; there are problems using events.