PDA

View Full Version : QThread, QWaitCondition and freeze



T4ng10r
2nd May 2010, 13:44
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


extern QWaitCondition robotNextMoveWait;
extern QMutex robotNextMoveWaitMutex;
...
robotNextMoveWaitMutex.lock();
emit robotBeforeMove(&currentScanResult);
robotNextMoveWait.wait(&robotNextMoveWaitMutex);
robotNextMoveWaitMutex.unlock();

this signal is transmitted through series of object Robot->enviroment->experiment->DataThread to second thread MainWindow->Dialog

in Gui Thread


QWaitCondition robotNextMoveWait;
QMutex robotNextMoveWaitMutex;
...


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


void CDataThread::run()
{
m_ptrExperiment = new CExperiment;
bool bResult;
bResult = connect(m_ptrExperiment,SIGNAL(robotBeforeMove(CSc anResults *)),SLOT(onRobotBeforeMove(CScanResults *)));
Q_ASSERT_X(bResult==true,"CDataThread::run","Connect 'm_stExperiment:robotBeforeMove ' with 'this:onRobotBeforeMove'");
//bResult = connect(m_ptrExperiment,SIGNAL(robotBeforeMove(CSc anResults *)),SIGNAL(robotBeforeMove(CScanResults *)));
//Q_ASSERT_X(bResult==true,"CDataThread::run","Connect 'm_stExperiment:robotBeforeMove ' with signal 'this:robotBeforeMove'");
exec();
}

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

squidge
2nd May 2010, 14:19
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.

T4ng10r
2nd May 2010, 14:31
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.