Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Mutithreaded program using signals and signal handler
janaki last edited by
i shud have 4 threads in my main program
- received_data – first threads receive messages
- process_data – whenever a message is received in first thread a signal should be sent to second thread for processing data – message id received from first thread should be used for processing data
- health_data – this thread sends health message for every 20 seconds
- display_data – this thread should display status in the gui.
i have written first thread which is working successfully, but am not getting idea how to send a signal when message is received and how to attach a signal handler when signal is received with the function in the second thread.
kindly suggest me for doing this
Have a look at the QThread and friends documentation, examples and demos. They explain nicely how to do that
janaki last edited by
thank u sir
but i should not use QT related functions
i should use c++ signals and signal handlers
kindly suggest me how to proceed
gvanvoor last edited by
You could use the signal slot implementaion of the boost library (be sure to use boost/signals2 as boost/signals (wihtout the 2) is not thread safe).
Typically you would do something like this (I have no idea what parameters you which to send in your signal, here I'm using an int):
// define the signature of your signal
typedef boost::signals2< void ( int ) > MySignalClass;
// create an instance
//connect a slot: the signature must match
boost::signals2::connection myConnection = mySignalInstance.connect( MySignalClass::slot_type );
// send a signal:
mySignalInstance( someIntValue );
geekgyrl last edited by
I know it's been a while since you asked this question, but you could install the signal handler before creating your threads. Then have only those threads that need to handle the signal do so. As for sending the signal, I suppose you would send it in the normal manner (depending on your OS/platform).
For example, before threads are created you would in main thread:
signal( <sig>, <sig_func> ); // installs the signal handler
<create your 4 threads...>
<join your threads and do any cleanup>
Remember to apply the appropriate sig_mask to those threads that should ignore the signal. So if thread_2 should handle the signal, then threads 1,3 and 4 should ignore it.
At least that's what I would do if I weren't using QThreads. Not sure that's the answer you were looking for, but I hope it helps.