PDA

View Full Version : Model / Threading Advice



tntcoda
27th June 2008, 21:19
Hi,

I have a QTableView based on a QAbstractTableModel and I need to introduce threads into my program, there will be no more than 10 threads and each thread needs to be able to get data from and send data to the QAbstractTableModel subclass.

The way i want to do it is by passing a pointer to the model to each thread, then make simple function calls to the model via the pointer, and make sure everything is properly locked with mutex's. This presents the possible problem though that the GUI thread may try to get data from the model when it is locked by a worker thread and block till its unlocked, will this have a noticeable impact on the QTableView / main GUI thread?

The second approach i have been advised of is to use signals/slots for cross thread communication, im not really sure how this would work for 2 way communication. I can understand i could send a signal from the worker thread to the model and do something, but i would need to get data back as well? Maybe i could pass a pointer to something as a parameter in the signal to be utilized from the model?

Are signals/slots thread safe by default?

I would really appreciate some advice on how i should design this, its phraphs not ideal having worker threads interact directly with a model but I think i will have to, i just need a simple/safe way to do it.

Thanks for any advice,

Jack

wysota
27th June 2008, 21:45
The way i want to do it is by passing a pointer to the model to each thread, then make simple function calls to the model via the pointer, and make sure everything is properly locked with mutex's.
Bad idea - slow and probably won't work anyway as you can't protect the internal implementation of the model.


The second approach i have been advised of is to use signals/slots for cross thread communication, im not really sure how this would work for 2 way communication. I can understand i could send a signal from the worker thread to the model and do something, but i would need to get data back as well?

Send another signal back to the calling object.


Maybe i could pass a pointer to something as a parameter in the signal to be utilized from the model?
Passing pointers across threads is a bad idea in general.


Are signals/slots thread safe by default?
Yes.


I would really appreciate some advice on how i should design this, its phraphs not ideal having worker threads interact directly with a model but I think i will have to, i just need a simple/safe way to do it.
It depends what do you need those threads for.

tntcoda
27th June 2008, 21:53
Bad idea - slow and probably won't work anyway as you can't protect the internal implementation of the model.



Send another signal back to the calling object.


Passing pointers across threads is a bad idea in general.


Yes.


It depends what do you need those threads for.

Thanks alot, the threads are basically are going to be performing operations with a QSslSocket based around the data that is retrieved from the model.

So am i right in thinking i would send a signal from the thread to the model, process it at a slot in the model and send a signal back to the worker, with some data in its parameters?

wysota
27th June 2008, 21:58
Thanks alot, the threads are basically are going to be performing operations with a QSslSocket based around the data that is retrieved from the model.
Ok, but why threads? Why not do it all in a single thread? Do you have 20 processing units in your machine that you want to use 20 threads?


So am i right in thinking i would send a signal from the thread to the model, process it at a slot in the model and send a signal back to the worker, with some data in its parameters?

Yes, this is possible.

tntcoda
27th June 2008, 22:02
Im using threads because i want to have up to 10 sockets downloading (synchronous io) simultaneously, is there a better approach than using threads for this purpose?

wysota
27th June 2008, 22:07
You can do that in a single thread. Qt doesn't use synchronous sockets, so you'll be using asynchronous downloading anyway. If you're not doing any heavy processing with the downloaded data, one thread will be sufficient.

derick
19th November 2008, 14:02
Hi,

Finally? Did we go for threads? How did it work out?