PDA

View Full Version : Can I pass data between threads with signal/slot, but without duplicating them?



hind
25th October 2013, 09:30
I have some big data to share between threads. While I don't want to use locks and unlocks everywhere, I chose the signal/slot pattern.
The best way I can think of is like:

emit updateData(const MyData &d)
But it's suggested that this avoids copying with DirectConnection but stills makes a copy with QueuedConnection. So what about BlockingQueuedConnection, which blocks and may work as a lock? If this is same with QueuedConnection for multi-thread, is there a way?
Thanks in advance!

wysota
25th October 2013, 10:31
If this data is really to be const (in both threads) then you can just emit a pointer. Otherwise if any of the parties involved can modify the object, you will eventually have to use locks.

hind
25th October 2013, 10:44
If this data is really to be const (in both threads) then you can just emit a pointer. Otherwise if any of the parties involved can modify the object, you will eventually have to use locks.
The emitter can modify the data but the receiver won't. Adding const to the parameter just makes sure the slot won't modify the data. I think this is safe because as far as I know it will send a copy of the data to the slot, no matter if it's a reference.

wysota
25th October 2013, 11:39
The emitter can modify the data but the receiver won't.
Then you have to either make a copy or synchronize access.