Results 1 to 3 of 3

Thread: QThread, QWaitCondition and freeze

  1. #1
    Join Date
    Feb 2007
    Location
    Wroclaw, Poland
    Posts
    72
    Thanks
    6
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Question QThread, QWaitCondition and freeze

    In main loop there's GUI processing. In second thread - created inside QMainWindow constructor - I want to process data.
    In specific point of time, in one of function in data thread - I want to display current process state and wait till user presses button (or timer expire - whatever). So I found QWaitCondition and QMutex which should do the job, but they only freeze program.

    In data thread
    Qt Code:
    1. extern QWaitCondition robotNextMoveWait;
    2. extern QMutex robotNextMoveWaitMutex;
    3. ...
    4. robotNextMoveWaitMutex.lock();
    5. emit robotBeforeMove(&currentScanResult);
    6. robotNextMoveWait.wait(&robotNextMoveWaitMutex);
    7. robotNextMoveWaitMutex.unlock();
    To copy to clipboard, switch view to plain text mode 
    this signal is transmitted through series of object Robot->enviroment->experiment->DataThread to second thread MainWindow->Dialog

    in Gui Thread
    Qt Code:
    1. QWaitCondition robotNextMoveWait;
    2. QMutex robotNextMoveWaitMutex;
    3. ...
    To copy to clipboard, switch view to plain text mode 

    In debug session I placed in all object in data thread slots with break point to watch how signal is processed. And it arrives to Experiment, but not to DataThread. Something strange.
    Connections in DataThread is set like this
    Qt Code:
    1. void CDataThread::run()
    2. {
    3. m_ptrExperiment = new CExperiment;
    4. bool bResult;
    5. bResult = connect(m_ptrExperiment,SIGNAL(robotBeforeMove(CScanResults *)),SLOT(onRobotBeforeMove(CScanResults *)));
    6. Q_ASSERT_X(bResult==true,"CDataThread::run","Connect 'm_stExperiment:robotBeforeMove ' with 'this:onRobotBeforeMove'");
    7. //bResult = connect(m_ptrExperiment,SIGNAL(robotBeforeMove(CScanResults *)),SIGNAL(robotBeforeMove(CScanResults *)));
    8. //Q_ASSERT_X(bResult==true,"CDataThread::run","Connect 'm_stExperiment:robotBeforeMove ' with signal 'this:robotBeforeMove'");
    9. exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    Where did I make error? Could some one show me better solution? This is my second contact with threads.

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread, QWaitCondition and freeze

    I'm not sure what you are trying to accomplish. If you are just sending a data block from one thread to another, and then sending back the results, there's no need for WaitConditions and Mutexs - you only need those if two threads could be working on the same data.

    Perhaps what would be better is some queued slots - you send the data via a signal, the data is processed and returned via another signal? Don't forget you will need to move the event handler from the caller to the thread using moveToThread.

  3. #3
    Join Date
    Feb 2007
    Location
    Wroclaw, Poland
    Posts
    72
    Thanks
    6
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread, QWaitCondition and freeze

    After second thread (data) is processing - data are processed in few nested loops. In the lowest loop - robot do something and I want to show what and why is he doing. So he's sending data to show them. AND he should wait till response arrives. Till then HE SHOULDN'T DO anything.
    So I need to freeze robot processing data, display his state, wait for user press button, then proceed to next step. It's done in loop - so I can't just send signal and return control higher - because this would mean that processing is over, which isn't true.

    Yes, I could use signals to process everything, but currently it would require to change lots of code due to architectural changes.I'm looking for something less invasive.

Similar Threads

  1. QThread event handling and QWaitCondition
    By mattc in forum Qt Programming
    Replies: 2
    Last Post: 21st August 2009, 13:00
  2. QWaitCondition and slot problem
    By satoshi in forum Qt Programming
    Replies: 0
    Last Post: 8th May 2009, 10:09
  3. Replies: 3
    Last Post: 30th June 2008, 15:23
  4. QWaitCondition
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 24th October 2007, 16:15
  5. QThread , GUI freeze
    By cs_raja in forum Qt Programming
    Replies: 4
    Last Post: 19th November 2006, 10:47

Tags for this Thread

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.