Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Mutithreaded program using signals and signal handler

    C++ Gurus
    4
    5
    2172
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J
      janaki last edited by

      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

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

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

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 0
        • J
          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

          1 Reply Last reply Reply Quote 0
          • G
            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
            MySignalsClass mySignalInstance;

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

            // send a signal:
            mySignalInstance( someIntValue );

            1 Reply Last reply Reply Quote 0
            • G
              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>
              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.

              1 Reply Last reply Reply Quote 0
              • First post
                Last post