[Qt-interest] Aborting QThreads

Jeffery MacEachern j.maceachern at gmail.com
Sat Jul 10 07:08:49 CEST 2010


I have a subclass of QThread (DataThread) which takes in data from an
external sensor and processes it.  Because of the nature of the
processing code - which is written in C - the actual processing
run-loop is outside of my thread class, and returns data points
periodically in a callback, which returns a bool to indicate whether
the run-loop should abort.  I've added a stop() method to my class,
with the following code:

void DataThread::stop()
{
    qDebug() << "Data Thread preparing to shut down";
    m_mutex.lock();
    m_abort = true;
    m_mutex.unlock();

    wait();
}

The destructor also makes use of this method, so that the hardware can
be deactivated gracefully.  What I'm wondering is whether it is more
"proper" to include the wait() call in the stop method for
convenience, or to expect the user of the thread to call it manually,
as they would otherwise have to.

Thanks,
    - Jeffery MacEachern




More information about the Qt-interest-old mailing list