[Development] qthread exiting

bradley.hughes at nokia.com bradley.hughes at nokia.com
Tue Aug 14 13:55:13 CEST 2012


On Aug 14, 2012, at 10:27 AM, ext Thiago Macieira wrote:

> On terça-feira, 14 de agosto de 2012 07.36.31, song.7.liu at nokia.com wrote:
>> Hi,
>> 
>> When the thread is exiting the destroy_current_thread_data will be called,
>> and only if the thread data is adopted, the QThreadPrivate::finish is
>> called. But normally the thread data always have the isAdopted with false,
>> then this thread 's status is still running even if it's exited.
>> 
>> So could somebody share when will the QThreadPrivate::finish be called
>> during thread exiting ? And what's the QAdoptedThread for? As only creating
>> the AdoptedThread then the thread data' isAdopted will be set as true.
> 
> Adopted threads are a weird concept you're likely not going to find. The 
> adopted threads were invented, as far as I remember, to support Qt Jambi, when 
> threads were created by the JVM instead of by QThread. But it allows one to 
> use QObjects and even event-loop items in threads not started with QThread.

Note also that the main() thread is an adopted thread.

> 
> QThreadPrivate::finish is run by pthreads cleanup solution. See 
> QThreadPrivate::start:
> 
>    pthread_cleanup_push(QThreadPrivate::finish, arg);
>    [...]
>    thr->run();
> 
>    pthread_cleanup_pop(1);
> 
> Here we have a perfect example of "The Boolean Trap", with a C twist.
> 
>       void pthread_cleanup_pop(int execute);
> 
>       The  pthread_cleanup_pop()  function  removes the routine at the
>       top of the stack of clean-up handlers, and  optionally  executes
>       it if _execute_ is nonzero.
> 
> So pthread_cleanup_pop calls finish(), and it's also called in case the thread 
> is cancelled.
> 
> As for destroy_current_thread_data, it's also run by pthreads, maybe at a 
> different moment. It's a callback by the pthread_specific, added by 
> pthread_key_create in create_current_thread_data_key.
> 
> -- 
> Thiago Macieira - thiago.macieira (AT) intel.com
>  Software Architect - Intel Open Source Technology Center
>     Intel Sweden AB - Registration Number: 556189-6027
>     Knarrarnäsgatan 15, 164 40 Kista, Stockholm, Sweden
> _______________________________________________
> Development mailing list
> Development at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/development

--
Bradley T. Hughes
bradley.hughes at nokia.com




More information about the Development mailing list