Monthly Archives: September 2010

The QThread anti-pattern

Sometimes with QThread you see something like this:

void run()
{
    while (true) {
        {
            QMutexLocker lock(&mMutexData);
            if (mQuit)
                break;
        }
 
        msleep (200);
    }
 
    mWaitExit.wakeAll();
}
 
void stop()
{
    mMutexData.lock();
    mQuit = true;
    mMutexData.unlock();
    mWaitExit.wait(&mMutexExit);
}

This is potentially problematic. Why? Consider the extreme case of a function shutdown() which does something like this:

void shutdown()
{
    thread->stop();
    delete thread;
}

Congratulations, you’ve just introduced a race condition. Why, you will ask? You’re waiting for the thread to end before deleting it, right?

No. And this has to do with the way Qt implements QThread::wait() which you should have probably used in the first place, if you really need this sort of functionality.

Qt hooks a pthread cleanup handler in the native thread which will call wakeAll() on a QWaitCondition stored inside the pimpl of QThread. And this pimpl – you might have guessed – is gone when you call delete.

Pino as in twitter

Since Echofon cut off custom-built Firefoxes I was looking for a new twitter client. The only one matching my needs was Pino. Unfortunately since the mandatory activation of OAuth in twitter it stopped working. But: No fear! I hacked it to use librest which supports OAuth ootb. Currently it is a bit flaky (crashes at startup sometimes for no obvious reason) and there is no easy way to log in. It currently uses librest’s demo consumer keys which is probably not the most fortunate thing to do. I will request own credentials as soon as it runs more stable. Stay tuned for updates, I will push it to a public repository really soon.

Update: Seems upstream pino will contain librest with OAuth in version 0.3. So I’ll keep my ugly hack to myself ;-)