在Linux上SetThreadPriority函数等效(pthreads的) [英] Equivalent of SetThreadPriority on Linux (pthreads)

查看:1938
本文介绍了在Linux上SetThreadPriority函数等效(pthreads的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于code以下位

,我在想,code相当于位会是怎样在linux假设pthreads的,甚至使用Boost.Thread API。

 的#include<&WINDOWS.H GT;诠释的main()
{
   SetThreadPriority函数(GetCurrentThread(),THREAD_PRIORITY_HIGHEST);
   返回0;
}


解决方案

等同于 SetThreadPriority函数在linux将 pthread_setschedprio(的pthread_t线程,INT优先级)

检查手册页

编辑:这里的样品code等同的:

 的#include< pthreads.h中>诠释的main()
{
    的pthread_t THID = pthread_self();
    pthread_attr_t thAttr;
    INT政策= 0;
    INT max_prio_for_policy = 0;    pthread_attr_init(安培; thAttr);
    pthread_attr_getschedpolicy(安培; thAttr,与策略);
    max_prio_for_policy = sched_get_priority_max(政策);
    pthread_setschedprio(THID,max_prio_for_policy);
    pthread_attr_destroy(安培; thAttr);    返回0;
}

本示例是这是SCHED_OTHER缺省的调度策略。

编辑:线程属性必须在使用之前必须初始化

Given the following bit of code, I was wondering what the equivalent bit of code would be in linux assuming pthreads or even using the Boost.Thread API.

#include <windows.h>

int main()
{
   SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_HIGHEST);
   return 0;
}

解决方案

The equivalent to SetThreadPriority in linux would be pthread_setschedprio(pthread_t thread, int priority).

Check the man page.

EDIT: here's the sample code equivalent:

#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;
}

This sample is for the default scheduling policy which is SCHED_OTHER.

EDIT: thread attribute must be initialized before usage.

这篇关于在Linux上SetThreadPriority函数等效(pthreads的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆