PDA

View Full Version : Passing data via signal/slot



gib
1st November 2010, 04:52
I want to pass a significant amount of data between threads, using the signal/slot mechanism. I was planning to pass a pointer to the data, but I have learned that this is frowned on. E.g. wysota wrote: "Never emit pointers (or non-const references) across threads, they might become invalid before the receiving thread has a chance to process them. Make a copy and send the copy instead."

I believe I can make sure that the pointer doesn't become invalid, but I don't like to go against the advice of someone much more expert than me. What would be the recommended approach when creating data copies is time-consuming? The app is transmitting data to update a VTK scene that has moving actors.

aamer4yu
1st November 2010, 04:57
If your data is huge, sending a copy might be expensive. In that case you may pass pointer. Though should be avoided and make sure the data is still valid in the slot.

Other thing you can do is emit a signal saying that data is ready. And provide some IPC mechanism for data transfer.

gib
1st November 2010, 05:28
Thanks. I don't know anything about IPC mechanisms. Could you provide a pointer?

tbscope
1st November 2010, 05:37
Or, keep the data in a file or other common place. Then use mutexes when accessing or writing data (or use qtconcurrent and don't have to deal with that complexity)

gib
1st November 2010, 05:49
QMutex and a data buffer in global scope looks like a good way to go. Simpler than shared memory.