Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Mutithreaded program using signals and signal handler
QtWS25 Last Chance

Mutithreaded program using signals and signal handler

Scheduled Pinned Locked Moved C++ Gurus
5 Posts 4 Posters 2.5k Views
  • 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 Offline
    J Offline
    janaki
    wrote on last edited by
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      0
      • J Offline
        J Offline
        janaki
        wrote on last edited by
        #3

        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
        0
        • G Offline
          G Offline
          gvanvoor
          wrote on last edited by
          #4

          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
          0
          • G Offline
            G Offline
            geekgyrl
            wrote on last edited by
            #5

            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
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved