PDA

View Full Version : Clearing the signal queue



grabalon
20th May 2010, 19:03
Hey All,

I have two signals that get emitted occasionally:

Load()
Unload()

These are connected with corresponding slots in a worker thread that will grab required data.
Is there a way, from the emitting function, to clear any Unload() signals from the signal queue? If it is not possible to clear just the unloads, is it possible to clear the entire queue?

Thanks

tbscope
20th May 2010, 19:06
You can disconnect signals and slots just like you connect them using QObject::disconnect();

SixDegrees
20th May 2010, 19:32
Once signals have been emitted, I don't know of any way to "recall" them. You could probably simulate such functionality through the use of, say, a global variable or singleton that would be set to a value indicating that no more signals should be processed, which the receiver would check at the beginning of each function call. Depending on the details of your implementation, it might be possible to send such information along with each signal itself instead of using a global.

As already noted, you can disconnect signals and slots, but that won't purge any signals that have already been sent.

wysota
20th May 2010, 20:26
This situation is not well suited for signals and slots. If you expect exactly one receiver for a signal and you know the receiver upfront then why not interact with it directly? Either post an event to the other object or implement some kind of a message queue between two objects.

grabalon
20th May 2010, 21:55
"If you expect exactly one receiver for a signal and you know the receiver upfront then why not interact with it directly?"

Signals, or events (I consider them equivalent when using a queued connection across threads) give me a simple means of passing information from my main thread to my worker thread. The problem I have is that the user can obsolete previously sent commands before the worker has had time to process them. Is it easier to clear out posted events than it is to clear a signal queue? Is there another message queuing structure within QT that would do a better job at what I am trying to do, or should I look at 3rd party libraries?

wysota
20th May 2010, 22:17
Is it easier to clear out posted events than it is to clear a signal queue?
Physically yes, logically no. You can send (not post but send) an event to the other thread to order it to ignore some other events. Of course you have to implement that all and you have no guarantees whether the other thread didn't manage to process those events in the meantime.

Is there another message queuing structure within QT that would do a better job at what I am trying to do, or should I look at 3rd party libraries?
Does everything have to be given on a silver platter? Can't you use QQueue with a mutex or other similarily simple solution?

squidge
20th May 2010, 23:07
What you need is to call methods directly which directly manage a list, then you have full control.

eg. in your header file:


QReadWriteLock listlock;
QList<myListType*> list;


then, in an append method:


listlock.lockForWrite();
list.append(foo);
listlock.unlock();


then you can have clear method, private read method, etc.

[This is only an example, there are lots of other ways to do it]

grabalon
20th May 2010, 23:41
The list idea is exactly what I need. Thanks to all for your help.