无限循环直到应用程序收到信号
我有一个启动 5 个线程的应用程序。
启动这些线程后,main() 中没有任何反应。
main(){
`start thread 1..5
}
如何在 main() 中无限循环,以便我的程序将连续运行,直到收到信号。
我不想使用,
while(true)
因为它会占用 CPU 周期。 (正如我所想)
编辑: 我正在使用海湾合作委员会4 线程API:pthread 操作系统:Linux
I have an application which starts 5 threads.
After starting those threads nothing happens in main().
main(){
`start thread 1..5
}
How do I loop infinitely in main() so my program will run continuously until it gets a signal.
I don't want to use
while(true)
because it will eat CPU cycle. (As I think )
Edit:
I am using gcc 4
Thread Api :pthread
OS : Linux
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
最简单的是:
最好的是join()所有五个线程。
the simplest would be:
and the best would be to join() all the five threads.
sigsuspend()
函数正是为此目的而设计的 - 它将挂起调用线程,直到它收到导致调用信号处理程序的信号。为了避免竞争条件(信号在进程调用
sigsuspend()
之前到达),您应该阻止该信号,检查它,然后将掩码传递给sigsuspend()
code> 解锁它:The
sigsuspend()
function is designed for precisely this purpose - it will suspend the calling thread until it recieves a signal that results in the calling of a signal handler.To avoid a race condition (where the signal arrives just before your process calls
sigsuspend()
), you should block the signal, check for it, then pass a mask tosigsuspend()
that unblocks it:加入这些线程请参阅 pthread_join。
Join those threads see pthread_join.
您可以尝试 Boost::Synchronization 函数, 像这样:
You could try the Boost::Synchronization functions, like this:
视窗?使用WaitForMultipleObjects。
Windows? Use WaitForMultipleObjects.