Linux 中的 WaitForSingleObject 和 WaitForMultipleObjects 等效吗?
我正在将一个应用程序从 Windows 迁移到 Linux。我面临着 WaitForSingleObject
和 WaitForMultipleObjects
接口的问题。
在我的应用程序中,我生成多个线程,其中所有线程等待来自父进程的事件或每 t 秒定期运行。
我已经检查了 pthread_cond_timedwait,但我们必须为此指定绝对时间。
我如何在 Unix 中实现这个?
I am migrating an applciation from windows to linux. I am facing problem with respect to WaitForSingleObject
and WaitForMultipleObjects
interfaces.
In my application I spawn multiple threads where all threads wait for events from parent process or periodically run for every t seconds.
I have checked pthread_cond_timedwait
, but we have to specify absolute time for this.
How can I implement this in Unix?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
坚持使用
pthread_cond_timedwait
并使用clock_gettime
。例如:如果您愿意,可以将其包装在函数中。
更新:根据我们的评论补充答案。
POSIX 没有像 Windows 那样的单个 API 来等待“所有类型”的事件/对象。每一项都有其自己的功能。通知线程终止的最简单方法是使用原子变量/操作。例如:
主线程:
辅助线程:
另一种选择是使用
pthread_cancel
发送取消请求。被取消的线程必须调用pthread_cleanup_push
来注册任何必要的清理处理程序。这些处理程序以与注册时相反的顺序调用。切勿从清理处理程序中调用pthread_exit
,因为这是未定义的行为。已取消线程的退出状态为PTHREAD_CANCELED
。如果您选择这种替代方案,我建议您主要阅读有关取消点和类型的信息。最后但并非最不重要的一点是,调用 pthread_join 将使当前线程阻塞,直到参数传递的线程终止。作为奖励,您将获得线程的退出状态。
Stick to
pthread_cond_timedwait
and useclock_gettime
. For example:Wrap it in a function if you wish.
UPDATE: complementing the answer based on our comments.
POSIX doesn't have a single API to wait for "all types" of events/objects as Windows does. Each one has its own functions. The simplest way to notify a thread for termination is using atomic variables/operations. For example:
Main thread:
Secondary thread:
Another alternative is to send a cancellation request using
pthread_cancel
. The thread being cancelled must have calledpthread_cleanup_push
to register any necessary cleanup handler. These handlers are invoked in the reverse order they were registered. Never callpthread_exit
from a cleanup handler, because it's undefined behaviour. The exit status of a cancelled thread isPTHREAD_CANCELED
. If you opt for this alternative, I recommend you to read mainly about cancellation points and types.And last but not least, calling
pthread_join
will make the current thread block until the thread passed by argument terminates. As bonus, you'll get the thread's exit status.无论如何,我们(NeoSmart Technologies)刚刚发布了一个开源(MIT 许可)库,名为 pevents其中实现POSIX 上的 WIN32 手动和自动重置事件,并包括 WaitForSingleObject 和 WaitForMultipleObjects 克隆。
虽然我个人建议您在 POSIX 机器上编码时使用 POSIX 多线程和信号范例,但如果您需要,pevents 会为您提供另一种选择。
For what it's worth, we (NeoSmart Technologies) have just released an open source (MIT licensed) library called pevents which implements WIN32 manual and auto-reset events on POSIX, and includes both WaitForSingleObject and WaitForMultipleObjects clones.
Although I'd personally advise you to use POSIX multithreading and signaling paradigms when coding on POSIX machines, pevents gives you another choice if you need it.
我意识到这是一个老问题了,但对于任何其他偶然发现它的人来说,这个来源表明 pthread_join() 实际上与 WaitForSingleObject() 做同样的事情:
http://www.ibm.com/developerworks/linux/library/l-ipc2lin1/index.html
祝你好运!
I realise this is an old question now, but for anyone else who stumbles across it, this source suggests that pthread_join() does effectively the same thing as WaitForSingleObject():
http://www.ibm.com/developerworks/linux/library/l-ipc2lin1/index.html
Good luck!
对于带有 false
WaitAll
的WaitForMultipleObjects
,请尝试以下操作:For
WaitForMultipleObjects
with falseWaitAll
try this: