PDA

View Full Version : Calling QThread::exec() repeatedly



hb
26th June 2007, 18:59
I have a problem with the event loop in a QThread. Say I have a class "Player" that inherits QThread. In the run() function, I have an infinite loop where the thread is put to sleep. Once it's woken up, it can do different things depending on the state of the class. One possibility is to call member functions of another class "Playback", which makes use of a QTimer, so I need to call the exec() function of the thread. Once the Playback's timer is finished with its stuff, it emits a signal that is caught by the thread class, which then calls quit() to get out of the event loop, and puts the thread to sleep again. This works very well when it is called the first time. On the second time, exec() returns immediately and no event loop is entered.

Is it possible to call exec() of a QThread repeatedly (but not concurrently)? In other words, can the following calling sequence be modified to work in a QThread class?:
exec() , quit(), exec()

Maybe by somehow "resetting" the event loop after the quit()?

Excerpt from the code:



// Player inherits QThread
void Player::run()
{
mutex.lock();
playback = new Playback();
QObject::connect(playback, SIGNAL(finishedPlayback()), this, SLOT(done()));
mutex.unlock();

while(1) {
mutex.lock();
cond.wait(&mutex);
mutex.unlock();

if(abort) {
mutex.lock();
delete playback;
playback = NULL;
mutex.unlock();
return;
}

if(status == Forward) {
// This call will start a QTimer, and emit finishedPlayback() when done
playback->forward(startTimestamp, endTimestamp, timestep);
exec();
}
}
}

void Player::done(void)
{
mutex.lock();
status = Finished;
mutex.unlock();
quit();
}

jpn
26th June 2007, 20:21
This bug has been fixed in Qt 4.3.0: 140734 - QThread, cant start exec more than once (http://trolltech.com/developer/task-tracker/index_html?method=entry&id=140734)

hb
26th June 2007, 20:24
Thank you for this information! Thinking the error was on my side, I've searched everywhere except bug trackers...