c++oding="utf-8" ?>
Linux下用pthread_getschedparam获取线程调度策略和优先级,Windows下用GetThreadPriority配合GetCurrentThread;二者均需平台特定处理,且std::thread须通过native_handle()转为原生句柄。

Linux下用pthread_getschedparam获取线程调度策略和优先级
在Linux上,C++线程(无论是std::thread还是原生pthread_t)的调度参数必须通过POSIX线程API获取,std::thread本身不提供访问接口。核心函数是pthread_getschedparam,它返回调度策略(如SCHED_FIFO、SCHED_OTHER)和静态优先级(sched_priority)。
注意:该调用只对当前线程有效(传nullptr或pthread_self()),且需链接-lpthread;若线程未显式设置过调度策略,通常返回SCHED_OTHER和优先级0(非实时策略下该值被忽略)。
#include <pthread.h>
#include <iostream>
int main() {
int policy;
struct sched_param param;
int ret = pthread_getschedparam(pthread_self(), &policy, ¶m);
if (ret == 0) {
std::cout <h3>Windows下用<code>GetThreadPriority</code>获取线程相对优先级</h3>
<p>Windows不暴露“绝对调度优先级”,而是使用基于进程基础优先级的相对偏移量。主线程默认继承进程优先级(通常是<code>THREAD_PRIORITY_NORMAL</code>),调用<code>GetThreadPriority</code>返回的是相对于进程基准的整数偏移(如-2到+2,或<code>THREAD_PRIORITY_HIGHEST</code>等常量)。</p>
<p>关键点:</p>
<ul>
<li>
<code>GetThreadPriority(GetCurrentThread())</code> 返回当前线程的相对优先级值</li>
<li>该值不能直接映射到Linux的<code>sched_priority</code>范围,也不反映CPU调度器实际使用的内部数值</li>
<li>若线程从未调用过<code>SetThreadPriority</code>,一般返回<code>THREAD_PRIORITY_NORMAL</code>(即0)</li>
<li>需要<code>#include <windows.h></windows.h></code>,且仅适用于Win32线程;<code>std::thread</code>底层可能封装为Win32线程,但标准不保证可移植性</li>
</ul>
<h3>为什么<code>std::thread::native_handle()</code>在不同平台表现不一致</h3>
<p><code>std::thread::native_handle()</code>返回类型是平台相关的:<code>pthread_t</code>(Linux/macOS)或<code>HANDLE</code>(Windows)。这意味着你无法跨平台统一调用调度API——必须条件编译。</p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/xiazai/gongju/2823" title="C++14"><img
src="https://img.php.cn/upload/manual/001/431/639/6ac8b33c327c4749.png" alt="C++14" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/xiazai/gongju/2823" title="C++14" class="overflowclass">C++14</a>
<p class="overflowclass">C++14 对 C++11 的修正与增强版本,适合旧系统维护和较老工具链兼容。</p>
</div>
<a rel="nofollow" href="/xiazai/gongju/2823" title="C++14" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div>
<p>常见误操作:</p>
<ul>
<li>直接对<code>std::thread</code>对象调用<code>pthread_getschedparam</code>(未先转<code>native_handle()</code>)→ 编译失败</li>
<li>在Windows上把<code>native_handle()</code>当<code>HANDLE</code>传给<code>GetThreadPriority</code>,却忘了<code>std::thread</code>构造时可能未使用Win32 API创建(例如某些libc++实现)→ 返回<code>THREAD_PRIORITY_ERROR_RETURN</code>
</li>
<li>假设所有线程默认有非零优先级 → 实际上绝大多数线程运行在<code>SCHED_OTHER</code>/NORMAL级别,优先级值无调度意义</li>
</ul>
<h3>实时优先级需要特权,普通用户调用会失败</h3>
<p>尝试获取或设置实时调度策略(如<code>SCHED_FIFO</code>)或高优先级时,<code>pthread_getschedparam</code>本身通常成功,但若此前曾用<code>pthread_setschedparam</code>设过实时策略而失败,后续获取到的仍是旧值——错误发生在设置阶段,而非读取阶段。</p>
<p>典型错误现象:</p>
<ul>
<li>
<code>pthread_setschedparam</code>返回<code>-1</code>且<code>errno == EPERM</code>:当前用户无<code>CAP_SYS_NICE</code>能力或未在<code>/etc/security/limits.conf</code>中配置<code>rlimit</code>
</li>
<li>即使root运行,内核参数<code>kernel.sched_rt_runtime_us</code>限制了实时带宽,也可能导致调度策略降级</li>
<li>macOS不支持<code>SCHED_FIFO</code>/<code>SCHED_RR</code>,强制回退到<code>SCHED_OTHER</code>,但<code>pthread_getschedparam</code>仍返回你“试图设置”的值,造成假象</li>
</ul>
<p>真正拿到的优先级,取决于你有没有权限改、系统允不允许、以及当前策略是否生效——读出来那个数字,未必是正在起作用的那个。</p></iostream></pthread.h>C++免费学习笔记(深入):立即使用
在学习笔记中,你将探索 C++ 的入门与实战技巧!










