QThread的源码(直接搜索"thread.cpp"即可,或者在github里搜)

Posted 朝闻道

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了QThread的源码(直接搜索"thread.cpp"即可,或者在github里搜)相关的知识,希望对你有一定的参考价值。

 

void QThread::run()
{
    (void) exec();
}

int QThread::exec()
{
    Q_D(QThread);
    QMutexLocker locker(&d->mutex);
    d->data->quitNow = false;
    if (d->exited) {
        d->exited = false;
        return d->returnCode;
    }
    locker.unlock();

    QEventLoop eventLoop;
    int returnCode = eventLoop.exec();

    locker.relock();
    d->exited = false;
    d->returnCode = -1;
    return returnCode;
}

void QThread::exit(int returnCode)
{
    Q_D(QThread);
    QMutexLocker locker(&d->mutex);
    d->exited = true;
    d->returnCode = returnCode;
    d->data->quitNow = true;
    for (int i = 0; i < d->data->eventLoops.size(); ++i) {
        QEventLoop *eventLoop = d->data->eventLoops.at(i);
        eventLoop->exit(returnCode);
    }
}

/*!
    Tells the thread‘s event loop to exit with return code 0 (success).
    Equivalent to calling QThread::exit(0).

    This function does nothing if the thread does not have an event
    loop.

    \sa exit(), QEventLoop
*/
void QThread::quit()
{ exit(); }

 

https://github.com/openwebos/qt/blob/master/src/corelib/thread/qthread.cpp

以上是关于QThread的源码(直接搜索"thread.cpp"即可,或者在github里搜)的主要内容,如果未能解决你的问题,请参考以下文章

Thread,QThread,每个进程有多少是“合理的”?

以“正确”的方式进行 QThread-ing

我如何从 QWidget 和 QThread 继承?

Qt线程—QThread的使用--run和movetoThread的用法

用 QThread 改变 QTextBrowser

QThread - 如何停止工人?