鉴于以下代码,我想知道在 linux 中假设 pthread 甚至使用 Boost.Thread API 的等效代码是什么。
#include <windows.h>
int main()
{
SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_HIGHEST);
return 0;
}
最佳答案
Linux 中的 SetThreadPriority
等价于 pthread_setschedprio(pthread_t thread, int priority)
。
查看man page .
编辑:这是等效的示例代码:
#include <pthread.h>
int main()
{
pthread_t thId = pthread_self();
pthread_attr_t thAttr;
int policy = 0;
int max_prio_for_policy = 0;
pthread_attr_init(&thAttr);
pthread_attr_getschedpolicy(&thAttr, &policy);
max_prio_for_policy = sched_get_priority_max(policy);
pthread_setschedprio(thId, max_prio_for_policy);
pthread_attr_destroy(&thAttr);
return 0;
}
此示例适用于默认调度策略 SCHED_OTHER。
编辑:线程属性必须在使用前初始化。
关于c++ - 等效于 Linux 上的 SetThreadPriority (pthreads),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10876342/