[Qt-interest] Example of "moveToThread (this)" idiom -- legal?

K. Frank kfrank29.c at gmail.com
Sun Jan 2 15:52:51 CET 2011


Hi Clint!

On Sat, Jan 1, 2011 at 10:40 PM, Clinton Stimpson <clinton at elemtech.com> wrote:
>
> For me, it is simply that QThread isn't documented as thread-safe or
> reentrant.  So, if a QThread moves itself off the original parent
> thread, I don't see how the original parent thread can call
> QThread::wait(), QThread::isFinished(), etc... without calling functions
> that aren't thread-safe.

That's a good point -- I hadn't thought of that.

That raises the question of whether QThread (or some of its
member functions) are not thread-safe, or actually are, but are
not so documented.  (I would expect some QThread member
functions to be thread-safe just because they don't really do
anything where a race condition could arise.  For example, as
a blocking call, QThread::wait is probably thread-safe, and as
a const member function that queries a simple boolean state,
I would expect QThread::isFinished to be thread-safe, as well.)

Taking into account the thread-safety issue, I do think my example
is still legal for the following reason:

The slot in my QThread-derived worker-thread class does
not call any of the QThread (base class) member functions,
or otherwise use or modify the QThread state.  (It does call
QThread::sleep, but that is a static function.)  So there shouldn't
be any race conditions relative to the work that the slot is doing.

> Clint

Thanks.


K. Frank


> On 01/01/2011 07:38 PM, K. Frank wrote:
>> Hello List!
>>
>> According to my understanding of the Qt threading model,
>> I believe that our so-called "moveToThread (this)" idiom is
>> technically legal.  I'd like to get some feedback on whether
>> this is correct.
>> ...



More information about the Qt-interest-old mailing list