Mutithreaded program using signals and signal handler



  • i shud have 4 threads in my main program

    1. received_data – first threads receive messages
    2. 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
    3. health_data – this thread sends health message for every 20 seconds
    4. 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


  • Lifetime Qt Champion

    Hi,

    Have a look at the QThread and friends documentation, examples and demos. They explain nicely how to do that



  • 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



  • 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
    MySignalsClass mySignalInstance;

    //connect a slot: the signature must match
    boost::signals2::connection myConnection = mySignalInstance.connect( MySignalClass::slot_type );

    // send a signal:
    mySignalInstance( someIntValue );



  • 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>
    exit(0);

    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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.