PDA

View Full Version : Using QEvent to avoid a slot to be called "concurrently"?



LisaDearheart
19th August 2012, 12:48
Hello,

in my application, I have to react to a specific signal by executing a slot. This slot can take some time to be executed, while multiple signals are often sent in a small amount of time. By putting some prints at the beginning and at the end of the slot, I noticed that in these cases the slot is called multiple times concurrently (but, as it appears, always in the same thread the receiver object lives in). This happens even if I use Qt::QueuedConnection in the connect() statements. I have to stop this and the force the second execution of the slot to wait for the first to be completed.

So instead of emitting signals I tried posting events to the event queue of the receiver object. Here are the most important parts of this code: http://paste.chakra-project.org/2619/ . But again I face the same issue as before: further events do not wait for the first event handler's execution to be completed, and instead start a new instance of the function immediately. How can I solve this?

Thanks for your time.

mvuori
19th August 2012, 13:23
Perhaps you could have a list or vector or whatever of your "things to do", from which you handle one at a time in a controlled manner. An added benefit of that would be a view to the amount of tasks stacking up.

LisaDearheart
19th August 2012, 14:00
That's an idea, yes, I shall definitely use that if I'm not able to find other solutions :)

amleto
19th August 2012, 17:05
"By putting some prints at the beginning and at the end of the slot, I noticed that in these cases the slot is called multiple times concurrently (but, as it appears, always in the same thread the receiver object lives in"

Please show a compilable example that displays this behaviour. This should not be happening so either you are mistaken or there is something wrong with your application. I recall someone had a similar problem that seemed to be caused by using a third party library, although the real reason was not unearthed iirc.

You should definitely find the cause and not work around it because you don't know what else could be going wrong.

LisaDearheart
19th August 2012, 19:34
Got it :) For now I'm not able to reproduce the exact behavior on a simpler code I can show you (mine is part of a big project) so I'm going to find out what is the mistake in my application.
Once I'll find it I'll let you know.

Added after 56 minutes:

Found it. I was calling QEventLoop::exec() on a function that was eventually called by my slot, and completely forgot about it. Once I removed that line everything seems to go as it should. Thanks for your input :)