通过POSIX信号终止C ++ 11中的多线程应用程序 [英] Terminating multithreaded application in C++11 by POSIX signal

查看:162
本文介绍了通过POSIX信号终止C ++ 11中的多线程应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Linux平台上在 C ++ 11 中写了一个简单的多线程应用程序,我想通过发送终止服务器及其运行线程,显然我的服务器应用程序使用来自C ++ 11的线程支持( std :: thread

code>等)。虽然我在C ++ 11( std :: signal )中发现了一些对信号处理的支持,但我在多线程环境中找不到任何处理信号的支持。



所以我的问题是 - 是否有任何方式如何处理多线程应用程序中的信号 C ++ 11

解决方案

只要因为我的应用程序需要处理信号,就可以依靠 pthreads

2.4信号概念


在生成时,将确定是否已为进程或进程内的特定线程生成信号。对于导致产生信号的线程,应该产生由可归因于特定线程的某些动作产生的信号,例如硬件故障。应该为进程生成与进程ID或进程组ID或异步事件(如终端活动)关联生成的信号。



...



为进程生成的信号应该传递到正在调用sigwait()函数选择该信号或​​没有阻塞传递的进程中的那些线程中的一个线程的信号。


鉴于上述,在多线程进程中,一个常见的解决方案是阻止所有信号在所有线程中处理一个。一个线程通常处理所有进程信号,并告诉其他线程要做什么(例如终止),并且通常是主线程。在创建其他线程之前,容易阻塞主线程中的信号,继承父线程的信号掩码。一旦主线程完成创建子线程并准备处理信号,它必须解除阻塞。



不幸的是,C ++ 11没有提供任何方法。你必须使用POSIX函数。在 pthread_sigmask中向下滚动到发送多线程过程中的信号 ,用于创建特殊信号处理线程的示例。如果您在主线程中使用可处理信号的事件循环,则只需在进入事件循环之前解除阻塞信号即可。后者不是必需的。


I wrote a simple multithreaded application in C++11 on Linux platform and I would like to terminate the server and its running threads by sending SIGINT signal.

Obviously my server application uses thread support from C++11 (std::thread etc.). Although I found some support for signal handling in C++11 (std::signal), I couldn't find any support for handling signals in multithreaded environment.

So my question is - is there any way how to handle signals in multithreaded application in C++11 or do I have to rely back on pthreads just because my application needs to deal with signals?

解决方案

2.4 Signal Concepts:

At the time of generation, a determination shall be made whether the signal has been generated for the process or for a specific thread within the process. Signals which are generated by some action attributable to a particular thread, such as a hardware fault, shall be generated for the thread that caused the signal to be generated. Signals that are generated in association with a process ID or process group ID or an asynchronous event, such as terminal activity, shall be generated for the process.

...

Signals generated for the process shall be delivered to exactly one of those threads within the process which is in a call to a sigwait() function selecting that signal or has not blocked delivery of the signal.

In the light of the above, in a multi-threaded process a common solution is to block all signals one intends to handle in all threads but one. That one thread would normally handle all process signals and tell other threads what to do (e.g. terminate) and is often the main thread. It easy to block the signals in the main thread before creating other threads, that inherit the signal mask of the parent thread. Once the main thread is done creating child threads and is ready to handle signals it must unblock those.

Unfortunately, C++11 does not provide any means for that. You have to use POSIX functions. Scroll down to Signalling in a Multi-Threaded Process in pthread_sigmask for an example that creates a special signal handling thread. The latter is not necessary if you are using an event loop in the main thread that can handle signals, just unblock the signals before entering the event loop.

这篇关于通过POSIX信号终止C ++ 11中的多线程应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆