macOS中pthread的QOS Class无法设置问题求助
macOS pthread QoS类设置不生效问题排查
问题描述
在macOS项目中使用C++的pthread创建线程,调用pthread_attr_set_qos_class_np设置QoS类时,函数返回成功,但调用pthread_attr_get_qos_class_np获取到的QoS class始终为0,尝试多种QoS值结果一致。
相关代码:
bool CppThread::CreateThread () { pthread_t threadId; pthread_attr_t attr; qos_class_t qos_class; int relative_priority; int rc = pthread_attr_init(&attr); if (rc) { printf ("pthread_attr_init failed"); } if (pthread_attr_set_qos_class_np (&attr, QOS_CLASS_BACKGROUND, 5)) { printf ("pthread_attr_set_qos_class_np failed\n"); } int retval = pthread_create(&threadId, &attr, threadFunction, NULL); if (retval) { std::cerr << "Error creating thread" << std::endl; return false; } if (pthread_attr_get_qos_class_np (&attr, &qos_class, &relative_priority)) { printf ("pthread_attr_get_qos_class_np failed\n"); } printf ("QoS class: %d, Relative priority: %d\n", qos_class, relative_priority); pthread_join(threadId, NULL); // Wait for thread to finish std::cout << "Thread finished" << std::endl; return true; }
输出结果:
QoS class: 0, Relative priority: 0
Thread finished
原因分析
问题的核心是传入的relative priority值超出了目标QoS类的合法范围:
QOS_CLASS_BACKGROUND对应的relative priority允许范围是0到-15(数值越小,优先级越低),你传入了正数5,属于无效值。- 虽然
pthread_attr_set_qos_class_np返回成功,但系统会忽略这个无效参数,导致QoS类未被正确设置到属性对象中。后续调用pthread_attr_get_qos_class_np时,自然返回默认的QOS_CLASS_UNSPECIFIED(对应数值0)。
解决方案
1. 使用合法的relative priority值
不同QoS类对应的relative priority范围不同,需严格遵守:
QOS_CLASS_USER_INTERACTIVE: 0 ~ 31QOS_CLASS_USER_INITIATED: 0 ~ 31QOS_CLASS_DEFAULT: 0 ~ -15QOS_CLASS_UTILITY: 0 ~ -15QOS_CLASS_BACKGROUND: 0 ~ -15
例如,将QOS_CLASS_BACKGROUND对应的relative priority改为0或-5:
pthread_attr_set_qos_class_np(&attr, QOS_CLASS_BACKGROUND, 0);
2. 正确验证QoS是否生效
建议在线程函数内部调用pthread_get_qos_class_np获取当前线程的实际QoS,而不是依赖创建线程时的attr对象(pthread_create会复制attr内容,后续attr状态不一定能反映线程实际属性)。
修改后的完整代码示例:
#include <pthread.h> #include <iostream> #include <cstdio> void* threadFunction(void* arg) { qos_class_t qos; int relative_prio; if (pthread_get_qos_class_np(pthread_self(), &qos, &relative_prio)) { printf("pthread_get_qos_class_np failed\n"); } else { printf("Thread actual QoS class: %d, Relative priority: %d\n", qos, relative_prio); } // 线程业务逻辑 return nullptr; } bool CppThread::CreateThread () { pthread_t threadId; pthread_attr_t attr; qos_class_t qos_class; int relative_priority; int rc = pthread_attr_init(&attr); if (rc) { printf("pthread_attr_init failed\n"); } // 使用BACKGROUND类合法的relative priority if (pthread_attr_set_qos_class_np(&attr, QOS_CLASS_BACKGROUND, 0)) { printf("pthread_attr_set_qos_class_np failed\n"); } int retval = pthread_create(&threadId, &attr, threadFunction, NULL); if (retval) { std::cerr << "Error creating thread" << std::endl; return false; } if (pthread_attr_get_qos_class_np(&attr, &qos_class, &relative_priority)) { printf("pthread_attr_get_qos_class_np failed\n"); } else { printf("Attr QoS class: %d, Relative priority: %d\n", qos_class, relative_priority); } pthread_join(threadId, NULL); std::cout << "Thread finished" << std::endl; return true; }
修改后预期输出:
Attr QoS class: 3, Relative priority: 0
Thread actual QoS class: 3, Relative priority: 0
Thread finished
(注:QOS_CLASS_BACKGROUND的数值定义为3,具体可查看<sys/qos.h>头文件)
内容的提问来源于stack exchange,提问作者Vipul Gupta
相关产品推荐
相关产品推荐

