PDA

View Full Version : How do I access data between threads



yodasoda
24th February 2010, 23:58
I want to use a QUDPSocket inside a QThread and I need data to be available to both the qthread and main thread. To be thread safe, do I need to send information from one thread to another via signals or can I just have them reside in either one of them and acces them from both threads. there is no chance that data will be accessed simultaneously.

franz
25th February 2010, 07:05
If you use signals and slots you also have to worry about thread safety. Although the signal/slot system itself is thread-safe, if you use direct connections across threads you still have to lock the data. If you are absolutely 100 % sure that your program isn't going to access the same data from different threads at the same time you can leave out locking. If there is a chance your code might, use locking. It's safer that way.

wagmare
25th February 2010, 07:06
I want to use a QUDPSocket inside a QThread and I need data to be available to both the qthread and main thread. To be thread safe, do I need to send information from one thread to another via signals or can I just have them reside in either one of them and acces them from both threads. there is no chance that data will be accessed simultaneously.

signals is the best way to transfer data from worker thread to main thread .... check blockingfortuneclient example ... it will help u a lot ...

yodasoda
26th February 2010, 19:10
thanks. I would like to talk about the difference of having a QThread be instantiated in QMainWindow and a QThread being instantiated in main.cpp. It seems there is a big difference between both and one is not a full separate thread in itself. Also, I would of like to use waitforreadyRead with QThread inside QMainWIndow but then the function sometimes gives me a "glibc detected double free exception"