thread 优先级linux,在Linux中pthread_create():什么是默认优先级和shceduling策略

惠普手册页(

link)说

“By default, the new thread’s scheduling policy and priority are

inherited from the creating thread—that is, by default, the

pthread_create(3) routine ignores the scheduling policy and priority

set in the specified thread attributes object. Thus, to create a

thread that is subject to the scheduling policy and priority set in

the specified thread attributes object, before calling

pthread_create(3) your program must use the

pthread_attr_setinheritsched(3) routine to set the inherit thread

attributes object’s scheduling attribute to PTHREAD_EXPLICIT_SCHED.”

这适用于Linux posix线程(NPTL pthread)吗?

如果没有,影响使用pthread_create()创建的线程的优先级和调度策略的因素是什么?

here开始:

The following values may be specified in inheritsched:

PTHREAD_INHERIT_SCHED

Threads that are created using attr inherit scheduling attributes from the creating thread; the scheduling attributes in attr are ignored.

PTHREAD_EXPLICIT_SCHED

Threads that are created using attr take their scheduling attributes from the values specified by the attributes object.

The default setting of the inherit scheduler attribute in a newly initialized thread attributes object is PTHREAD_INHERIT_SCHED.

因此,似乎与问题中描述的行为相同.

我实际上已经认为这个默认值可以由标准定义(虽然我没有那么多经验,但我承认),但是看看Thread Scheduling Attributes部分的最后一行:

(…) The default values of other scheduling attributes are implementation-defined.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在`pthread_create`函数设置线程的优先级,你可以在创建线程之前使用`pthread_attr_init`函数来初始化线程属性,然后使用`pthread_attr_setschedparam`函数设置线程的调度参数。 下面是一个示例代码: ```c++ #include <pthread.h> #include <iostream> void* IntputThread(void* arg) { // 线程函数的实现 return NULL; } int main() { pthread_t m_threadID; pthread_attr_t attr; sched_param param; // 初始化线程属性 pthread_attr_init(&attr); // 设置线程为可连接的(joinable) pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); // 设置线程优先级 int policy = SCHED_FIFO; // 使用FIFO调度策略 param.sched_priority = 10; // 设置优先级为10(取值范围为0-99,值越大优先级越高) pthread_attr_setschedpolicy(&attr, policy); pthread_attr_setschedparam(&attr, &param); // 创建线程 int ret = pthread_create(&m_threadID, &attr, IntputThread, NULL); if (ret != 0) { std::cerr << "Failed to create thread." << std::endl; return 1; } // 等待线程结束 pthread_join(m_threadID, NULL); // 销毁线程属性 pthread_attr_destroy(&attr); return 0; } ``` 在上面的示例,我们将`IntputThread`函数作为线程函数传递给`pthread_create`函数,并使用`pthread_attr_setschedparam`函数来设置线程的优先级。请注意,`pthread_attr_setschedparam`函数需要传递一个`sched_param`结构体参数,其`sched_priority`字段表示线程的优先级。 记住,只有具有足够权限的用户才能设置较高的优先级。另外,需要注意的是,设置线程的优先级可能会受到系统限制,因此并不总是能够按要求设置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值