[PATCH] thread-safe KQueuedDialog episode 2

Matthias Kretz kretz at kde.org
Thu Nov 27 10:49:12 GMT 2008


On Thursday 27 November 2008 10:35:42 Sebastian Trueg wrote:
>  KDialogQueue* KDialogQueue::self()
>  {
>    K_GLOBAL_STATIC(KDialogQueue, _self)
> +
> +  // we want KDialogQueue to be thread-safe, i.e. it should be possible
> +  // to queue dialogs from any thread, not only the GUI thread. For that
> +  // to work, the KDialogQueue has to live in the GUI thread. Only then
> +  // the queued connections used below will end up there.
> +  if(_self->thread() != QApplication::instance()->thread() &&
> +     _self->thread() == QThread::currentThread())
> +      _self->moveToThread(QApplication::instance()->thread());
> +

AFAICS you can move this code into the KDialogQueue constructor. That way 
you're guaranteed to get a KDialogQueue object that is linked to the GUI 
thread from all threads. Otherwise you have a race condition where two threads 
access this function, the non-GUI thread creates the object and the other 
thread starts to use it before the thread that created the object moves it to 
the GUI thread.
Also you then have to do the thread affinity check only once instead of for 
every access.

>    return _self;
>  }

Matthias Kretz (Germany)                            <><

More information about the kde-core-devel mailing list