Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Signaling all threads

Signaling all threads

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.6k 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.
  • S Offline
    S Offline
    seanr8
    wrote on last edited by
    #1

    I'm looking for a way to emit a signal that will be heard by all the threads of a certain type. Basically, I have code where when one thread emits a signal, the only thread of that type that doesn't need to listen to it is the thread that emitted it. Is there any way of doing this? I tried using @connect(this, SIGNAL(mySignal()),threadType,SLOT(mySignalHandler()));@
    but obviously this was a syntax error.

    I've toyed with the idea of consistently polling a shared variable, but I don't want to waste so much of the CPU. I can see that infinite loop in each thread only causing rampant CPU usage. Is there a way to implement this with a timer? I've never used a timer before.

    Thanks,
    Sean

    1 Reply Last reply
    0
    • S Offline
      S Offline
      seanr8
      wrote on last edited by
      #2

      I should probably say, when the signal goes, it tells all the threads to check on a shared buffer between them for new data that pertains to them. If they discover data with their ID, then they send this data over their TCP socket. I'm explaining this because I tried to use the QTimer class to have a timer signal that the thread should check the data buffer for data, but when it finds data and then tries to send it, I get a "QSocketNotifier: socket notifiers cannot be enabled from another thread"

      I do not understand what this means. Does it mean that the socket of one thread cannot send data that was brought in from another thread? Or does this mean that the timer (which I think is technically a thread) cannot initiate a signal that will cause the socket to send data?

      1 Reply Last reply
      0
      • S Offline
        S Offline
        seanr8
        wrote on last edited by
        #3

        Just in case anyone wondered what the answer was:

        QSocketNotifier prevents threads other than the thread to which the socket belongs to initiate a signal that will cause the socket to write (and I'd assume read, but I haven't seen any evidence of this). To get around it, I added a new slot and signal. The timer called the slot, which emitted the new signal. This new signal called the write slot and to QSocketNotifier it appears that the signal that initiated the write was a signal that was produced by the thread that owns the socket. Problem dodged.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          KA51O
          wrote on last edited by
          #4

          Another possible solution without a timer and using signal slots could be that you connect all your threads to the signal (like in your first post). Of course all threads get that signal and as I understood you want all threads except for the one that emit the signal to handle it.
          Easiest solution would be to check in the connected slot if the sender is equal to the receiver.
          @
          if(!sender() == this)
          {
          //handle signal
          }
          @

          For more information on this see "QObject::sender()":http://qt-project.org/doc/qt-4.8/qobject.html#sender

          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