1.void setPriority(Priority priority);
void QThread::setPriority(Priority priority)
{
Q_D(QThread);
QMutexLocker locker(&d->mutex);
if (!d->running) {
qWarning("QThread::setPriority: Cannot set priority, thread is not running");
return;
}
d->setPriority(priority);
}
void QThreadPrivate::setPriority(QThread::Priority threadPriority)
{
priority = threadPriority;
// copied from start() with a few modifications:
#ifdef QT_HAS_THREAD_PRIORITY_SCHEDULING
int sched_policy;
sched_param param;
if (pthread_getschedparam(from_HANDLE<pthread_t>(data->threadId.loadRelaxed()), &sched_policy, ¶m) != 0) {
// failed to get the scheduling policy, don't bother setting
// the priority
qWarning("QThread::setPriority: Cannot get scheduler parameters");
return;
}
int prio;
if (!calculateUnixPriority(priority, &sched_policy, &prio)) {
// failed to get the scheduling parameters, don't
// bother setting the priority
qWarning("QThread::setPriority: Cannot determine scheduler priority range");
return;
}
param.sched_priority = prio;
int status = pthread_setschedparam(from_HANDLE<pthread_t>(data->threadId.loadRelaxed()), sched_policy, ¶m);
# ifdef SCHED_IDLE
// were we trying to set to idle priority and faile

本文详细介绍了QThread类的几个关键函数,包括设置线程优先级、堆栈大小、启用/禁用终止以及线程等待等。这些函数在多线程编程中用于管理和同步线程,确保线程安全和高效运行。同时,代码展示了如何处理错误情况,如尝试在未运行的线程上设置优先级或改变堆栈大小。
最低0.47元/天 解锁文章

1869

被折叠的 条评论
为什么被折叠?



