PDA

View Full Version : qThread and Unix Signals



peterjb31
7th April 2011, 12:10
Hi

I am having problems catching unix signals in a multithreaded application. The signals are caught fine unless I start a thread at which point the default signal handling occurs.

To create signal handlers I am using the following code which is located in main:

signal(SIGINT, shutdown);

This should cause the shutdown function to run on a SIGINT, however when I start a thread the above no longer works as expected. The following code is being used to start the thread:

XmlRpcServer* xmlServer = new XmlRpcServer;
QThread xmlServerThread;
xmlServer->moveToThread(&xmlServerThread);
xmlServer->connect(&xmlServerThread,
SIGNAL(started()),
SLOT(startServer()));
xmlServerThread.start();

I'd be grateful for any advice anyone can offer as this has been stumping me for a while.

wysota
7th April 2011, 12:24
There is a whole theory on using unix signals with threads so you should first learn all that. In short it's safest to install a handler for a signal in one thread and mask out this signal in all other threads or install the same handler in all threads. The reason is that signal handlers are associated with a thread so if the signal occurs when another thread is active they will not use those custom handlers unless the thread inherited the handler bitmap from its parent thread (which would require the signal handler to be set before the new thread was spawned and assuming the operating system copies the signal mask to the new thread when clone() or equivalent is called).