Passing data via signal/slot
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.
Re: Passing data via signal/slot
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.
Re: Passing data via signal/slot
Thanks. I don't know anything about IPC mechanisms. Could you provide a pointer?
Re: Passing data via signal/slot
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)
Re: Passing data via signal/slot
QMutex and a data buffer in global scope looks like a good way to go. Simpler than shared memory.