Results 1 to 7 of 7

Thread: Model / Threading Advice

  1. #1
    Join Date
    Jun 2008
    Location
    UK
    Posts
    35
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Model / Threading Advice

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Model / Threading Advice

    Quote Originally Posted by tntcoda View Post
    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.

  3. #3
    Join Date
    Jun 2008
    Location
    UK
    Posts
    35
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Model / Threading Advice

    Quote Originally Posted by wysota View Post
    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?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Model / Threading Advice

    Quote Originally Posted by tntcoda View Post
    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.

  5. #5
    Join Date
    Jun 2008
    Location
    UK
    Posts
    35
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Model / Threading Advice

    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?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Model / Threading Advice

    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.

  7. #7
    Join Date
    Jan 2006
    Location
    France
    Posts
    36
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Model / Threading Advice

    Hi,

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

Similar Threads

  1. hierarchical model in a flat view
    By gniking in forum Qt Programming
    Replies: 4
    Last Post: 10th November 2009, 20:17
  2. Coin3d + Qt: SIGLNALs and SLOTs
    By vonCZ in forum Newbie
    Replies: 26
    Last Post: 15th May 2009, 07:34
  3. Model Choices review/questions
    By ucntcme in forum Qt Programming
    Replies: 1
    Last Post: 16th September 2007, 21:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.