April 9, 2012

aekam aekam
Lab Rat
87 posts

*[SOLVED]* Multiple signals and single slot…

 

Hello,
I have written a code in which a slot is connected to multiple signals of different threads.
Now there is a possibility of simultaneous emission of signals.

So what I am thinking is, since the connection type is Qt::AutoConnection and senders and receiver are in different threads, signals emitted will be delivered one by one to the receiver’s event loop, i.e. after the execution of slot is completed for a signal… hence the action against the signal will not be parallel…
Am I right.??

 Signature 

If you take care of inches, you won’t have to worry about miles… :)

10 replies

April 9, 2012

Andre Andre
Robot Herder
6296 posts

Yes. The cross-thread signals will go via the event loop. Events are put on the event queue of the eventloop for the thread of the receiving object. That queue is protected by a mutex (I think, it might be some other synchronization mechanism, I did not check the sources). That means that signal invokations will become serialized.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

April 9, 2012

aekam aekam
Lab Rat
87 posts

That means that signal invokations will become serialized.

does this mean that the execution of pending signals will be done only after the execution of current slot is completed.??
what happens in case where the slot is waiting for something (wait state) or goes into sleep for sometime (sleep or msleep) [ideally it should not be like this, asking just for knowledge], will the execution of next signal will be carried out, in parallel??

The thing I am concerned about is protection of a resource which is being operated from the slot [single] upon emission of signal [multiple].

 Signature 

If you take care of inches, you won’t have to worry about miles… :)

April 9, 2012

Andre Andre
Robot Herder
6296 posts

Yes, in a single thread, the execution is only at a single point of your code. That includes handling signals… If you suspend that thread, the whole thread is suspended.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

April 10, 2012

aekam aekam
Lab Rat
87 posts

ok… i wrote a sample code for this.

and what i understood is, in case of simultaneous emission of signals (connected to single slot), the execution of the slot will be non-blocking (i.e. signals will be queued up to be executed later, if slot is already serving any signal and emitter thread will continue execution) and slot will respond to the signals, one by one, as per their order in event queue.

Am I right.??
I found the same behavior in case where the SIGNAL is in C++ and SLOT is in Qml. [also checked with sample code]

 Signature 

If you take care of inches, you won’t have to worry about miles… :)

April 10, 2012

Andre Andre
Robot Herder
6296 posts

Not sure what you mean here. Simultaneous emission of signals can only be across threads. Within a single thread, that is impossible. Across thread signals are by default indeed non-blocking: the emitting thread just continues work, the receiving one will pick off the corresponding event from the queue when there is time for it, and process them one by one.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

April 10, 2012

aekam aekam
Lab Rat
87 posts

Yes, I do mean the different thread… this is rough outline of the program I tested.

  1. class SignalThread : public QThread
  2. {
  3. public:
  4.      SignalThread ()
  5.      {
  6.      }
  7.  
  8.      void run ()
  9.      {
  10.           while (1)
  11.           {
  12.                emit someSignal ();     // emits signal
  13.                msleep (1);               // for context switching
  14.           }
  15.      }
  16.  
  17. signals:
  18.      void someSignal ();
  19. }
  20.  
  21. class SlotClass
  22. {
  23. public:
  24.      SlotClass ()
  25.      {
  26.           for (int index = 0; index < MAX_SIGNALS; ++ index)
  27.           {
  28.                connect (&signalThread [index], SIGNAL (someSignal ()), this, SLOT (someSlot ()));
  29.                signalThread [index].start ();
  30.           }
  31.      }
  32. private:
  33.      SignalThread signalThread [MAX_SIGNALS];
  34. public slots:
  35.      void someSlot ()
  36.      {
  37.           qDebug () << "signal received";
  38.      }
  39. }

with event loop running in main function, i.e. a.exec ();

thanks for the guidance, it is helping me.

 Signature 

If you take care of inches, you won’t have to worry about miles… :)

April 10, 2012

aekam aekam
Lab Rat
87 posts

sorry, I forgot to mention…
I had taken multiple instances of signal class, which emits signals in while loop, to obtain simultaneous emission.

 Signature 

If you take care of inches, you won’t have to worry about miles… :)

April 10, 2012

Andre Andre
Robot Herder
6296 posts

I guess you’re just blogging on what you’re doing now? Or do you have a question on all that?

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

April 10, 2012

aekam aekam
Lab Rat
87 posts

yes, just updated the code for sake of clarity.
My question is solved. thanks. :)

 Signature 

If you take care of inches, you won’t have to worry about miles… :)

April 10, 2012

mlong mlong
Robot Herder
1551 posts

Be sure and edit the thread title to add [Solved]. Thanks!

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

 
  ‹‹ Debugging paint event = segmentation fault?      Object::connect: No such slot QGroupBox::lcs_slot(bool) ››

You must log in to post a reply. Not a member yet? Register here!