Results 1 to 6 of 6

Thread: The right approach to "ask" data to the running thread..

  1. #1
    Join Date
    Dec 2007
    Location
    Ancona, Italy
    Posts
    24
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Cool The right approach to "ask" data to the running thread..

    Hello.. I have a Qt application (an agent-based model) with a GUI thread and a "model" thread that runs for hours.

    I managed to get a way to stop/pause/resume/ the model thread from the GUI, and also to pass data from the model thread to the GUI for run-time visualisation

    However now I'd like to "ask" the model thread for a specific information (values for a plot, passing as parameter an integer), and then the model thread would give back the info to the GUI in the usual manner..

    The problem is that it doesn't work .. when I am inside a function of the model thread ( a QThread derived class) called from the GUI I got different values than if I run the same function from the normal model "path".. like they are different instances, or ??...

    Which is the right approach to do something like that?? When I am going ask the model thread for data I just want "read" them..

    ciao.. and happy 2008
    Sylvaticus

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: The right approach to "ask" data to the running thread..

    Did you start the thread event loop with exec()? In this case you can send a signal from the GUI thread to the worker thread, basically telling it you need some data. Then the worker thread will send a signal to the GUI thread, with some parameters. This is the safest way.

    If you didn't start the event loop and don't want to redesign your code now, then you must have an endless loop in your thread's run method. But you'll have to post some code, or at least describe the way you do things better.

    The problem is that it doesn't work .. when I am inside a function of the model thread ( a QThread derived class) called from the GUI I got different values than if I run the same function from the normal model "path".. like they are different instances, or ??...
    In that function, do you perform some specific thread processing? Because the function will be executed the GUI thread context. This could explain why the members/variables are messed up.

    and happy 2008
    You too.

  3. The following user says thank you to marcel for this useful post:

    sylvaticus (1st January 2008)

  4. #3
    Join Date
    Dec 2007
    Location
    Ancona, Italy
    Posts
    24
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: The right approach to "ask" data to the running thread..

    I think I got it.. I'll clean the code (I made a mess while tempting) and then I will post here if someone is interested ;-)))

    Many thanks,
    Sylvaticus

  5. #4
    Join Date
    Dec 2007
    Location
    Ancona, Italy
    Posts
    24
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Cool Re: The right approach to "ask" data to the running thread..

    ok.. this is what I done so far.. it took me longer as the first approach (query executed directly by the GUI trough a signal) was working without problems in Linux but randomly crash in Windows..
    Does the following looks as a good approach or it is advisable to be done in an other way??

    I have three classes involved, a MainWindow (QMainWindow derived), ThreadManager (QThread derived) and MainProgram (independent class with the model code).

    MainWindow starts the thread calling QThread::start().

    The QThread::start() call really executes the code in the ThreadManager::run() function that contain MainProgram::run() with all the model code.

    The pause and stop system is implemented with the bool ThreadManager::running and bool ThreadManager::stopped variables.
    Often (every few seconds) the model code goes to check the status of that variables that can be changed also from the GUI. The code is:
    Qt Code:
    1. void
    2. ThreadManager::refreshGUI(){
    3. checkQuery(0,0,false);
    4. while (!running){
    5. if(stopped){
    6. break;
    7. }
    8. }
    9. if (stopped){
    10. emit upgradeLogArea("Model has been stopped.");
    11. running= false;
    12. throw(2);
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    checkQuery(int, int, bool) is a function that can be called both from the GUI trough a signal or from the running thread trough refreshGUI(), and it is so protected with a QMutex.
    It's role is to change/control the status of the query parameters that are members of ThreadManager. The third function parameter is a bool that tell the function if the query parameters should be set (if the query come from the GUI) or if the query should be executed and its results sent back to the GUI with a signal (if the call come from the running thread).
    The code is :
    Qt Code:
    1. void
    2. ThreadManager::checkQuery(int px_ID, int currentLayerIndex, bool newRequest){
    3. QMutexLocker locker(&mutex);
    4. if(newRequest){
    5. pxQueryID = px_ID;
    6. layerQueryPos = currentLayerIndex;
    7. if(stopped){computeQuery(pxQueryID, layerQueryPos);layerQueryPos = -1;} // model is stopped, no way the model thread will do the query work
    8. else{emit publishQueryResults("<i>..wait.. processing query..</i>");} // model is running.. it will be the model thread to execute the query
    9. return;
    10. } else {
    11. if(layerQueryPos<0){
    12. return;
    13. } else {
    14. computeQuery(pxQueryID, layerQueryPos);
    15. layerQueryPos = -1;
    16. return;
    17. }
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

    The whole code is located at:
    http://www.regmas.org/doc/referenceM...ainWindow.html
    http://www.regmas.org/doc/referenceM...adManager.html
    http://www.regmas.org/doc/referenceM...inProgram.html

    Thank you.. I think I finally found a pleace to discuss the Qt programming.. the mailing list looks too technical for a beginner like me...

    Cheers...
    Sylvaticus

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: The right approach to "ask" data to the running thread..

    The problem might be in automatic connections. QThread object lives in the GUI thread, so automatic connections between widgets and the thread object will behave like if they were direct, which might cause troubles.

    This problem was discussed several times, so please search the forum. A few examples:
    http://www.qtcentre.org/forum/f-qt-p...eads-6060.html
    http://www.qtcentre.org/forum/f-qt-p...read-3943.html

  7. #6
    Join Date
    Dec 2007
    Location
    Ancona, Italy
    Posts
    24
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: The right approach to "ask" data to the running thread..

    Thanks for the explanation & the links.....

Similar Threads

  1. Accessing data from a worker thread
    By steg90 in forum Qt Programming
    Replies: 20
    Last Post: 25th May 2007, 10:20
  2. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  3. QThread: Destroyed while thread is still running
    By Shuchi Agrawal in forum Newbie
    Replies: 8
    Last Post: 3rd April 2007, 06:27
  4. Replies: 10
    Last Post: 20th March 2007, 22:19

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.