Qt线程之QThread

线程结构

以linux平台为例

QThreadPrivate
+ QMutex mutex
+ QAtomicInt quitLockRef
+ bool running
+ bool finished
+ bool isInFinish
+ atomic<bool> interruptionRequested
+ bool exited
+ int returnCode
+ uint stackSize
+ QThread::Priority priority
+ QWaitCondition thread_done
+ QThreadData *data
QThreadData
- QAtomicInt _ref
+ int loopLevel
+ int scopeLevel
+ QStack<QEventLoop *> eventLoops
+ QPostEventList postEventList
+ QAtomicPointer<void> threadId
+ QAtomicPointer<QThtread> thread
+ QAtomicPointer<QAbstractEventDispatcher> eventDispatcher
+ QVector<void *> tls
+ FlaggedDebugSignatures flaggedSignatures
+ bool quitNow
+ bool canWait
+ bool isAdopted
+ bool requiresCoreApplication
QThread

线程创建

  • 构造函数
public:
	explicit QThread(QObject *parent = nullptr);
protected:
	QThread(QThreadPrivate &dd, QObject *parent = nullptr);

线程优先级

枚举值有

  • IdlePriority
  • LowestPriority
  • LowPriority
  • NormalPriority
  • HighPriority
  • HighestPriority
  • TimeCriticalPriority
  • InheritPriority
enum Priority {
        IdlePriority,

        LowestPriority,
        LowPriority,
        NormalPriority,
        HighPriority,
        HighestPriority,

        TimeCriticalPriority,

        InheritPriority
    };

class QThreadPrivate
{
	QThread::Priority priority;
}

设置优先级

void setPriority(Priority priority);

获取优先级

Priority priority() const;

线程运行状态

    bool isFinished() const;
    bool isRunning() const;
class QThreadPrivate 
{
	bool running;
    bool finished;
    bool isInFinish; //when in QThreadPrivate::finish
} 

线程栈

    void setStackSize(uint stackSize);
    uint stackSize() const;
class QThreadPrivate 
{
	uint stackSize;
} 

线程的事件分发器

QAbstractEventDispatcher *eventDispatcher() const;
void setEventDispatcher(QAbstractEventDispatcher *eventDispatcher);

线程的控制

public Q_SLOTS:
    void start(Priority = InheritPriority); //线程开启
    void terminate();//结束线程
    void quit();//结束线程的QEventLoop

线程的等待

bool wait(unsigned long time = ULONG_MAX);

QThread调用start具体做了哪些工作

以linux平台为例,其时序为

user QThread QThreadPrivate 系统 start start pthread_cleanup_push(QThreadPrivate::finish) 设置QThreadData的threadId以及线程本地存储数据 emit started(QThread::QPrivateSignal()) run exec pthread_cleanup_pop finish user QThread QThreadPrivate 系统

其创建的是脱离线程的,即属性为PTHREAD_CREATE_DETACHED

  • 创建线程执行QThreadPrivate的start方法
    • 触发线程的started信号 emit thr->started(QThread::QPrivateSignal());
    • 执行线程的默认run方法thr->run()
      • 执行exec方法
        • 执行事件循环,调用QEventLoop的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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值