[Development] 回复: Re: qthread exiting

song.7.liu at nokia.com song.7.liu at nokia.com
Tue Aug 14 12:02:33 CEST 2012


Ok, these two pthrea_cleanup_push/pop are not implemented in our pthread, but i think it should be easy to add them. Thanks !

-----原信息-----
发件人: ext Thiago Macieira
发送:  2012/08/14, 16:28
收件人: development at qt-project.org
主题: Re: [Development] qthread exiting


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.

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


More information about the Development mailing list