2012-06-04 15 views

답변

22

리눅스에서 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이다 기본 스케줄링 정책입니다 : 여기에 샘플 코드에 해당합니다.

EDIT : 사용 전에 스레드 속성을 초기화해야합니다.

+1

()'. – caf

+0

@caf 힌트를 보내 주셔서 감사합니다. 코드 스 니펫을 업데이트했습니다. –

+4

POSIX 표준 (제공된'phtread__setschedprio (3)'매뉴얼 페이지에서 http://pubs.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_08에 이르는 링크를 따라 가면서).html # tag_02_08_04_01)는'SCHED_OTHER' 정책에서 실행중인 쓰레드에'pthread_setschedprio (3)'을 사용한다고 언급하고 있습니다. 리눅스에서는 우선 순위 값의 범위가'[0, 0]'이므로 실제 답변을 사용하지 않는 한 리눅스에서는 쓸모가 없습니다 (SCHED_FIFO' 또는'SCHED_RR')에 대한 질문에 의해 호출되지 않습니다. – FooF

3

같은 것이고 pthread_setschedparam()과 정책 및 우선 순위의 조합입니다.

SCHED_FIFO, SCHED_RR 정책의 스레드 우선 순위를 지정할 수 있습니다.

16

당신이 원하는 : 여러 다른 답변에서 언급 한 바와 같이

#include <pthread.h> 

int main() 
{ 
    int policy; 
    struct sched_param param; 

    pthread_getschedparam(pthread_self(), &policy, &param); 
    param.sched_priority = sched_get_priority_max(policy); 
    pthread_setschedparam(pthread_self(), policy, &param); 

    return 0; 
} 
5

POSIX 표준은 pthread_setschedparam(3)이 포함되어 있습니다. 대부분이 POSIX 스레드 라이브러리 함수는 실시간 스레드에 대해 언급 할 때 언급되지만 POSIX 표준은 실시간 스레드의 도메인에만 사용하도록 제한하지 않습니다. 그러나 Linux에서는 실시간 스케줄링 클래스 SCHED_FIFO 또는 SCHED_RR을 사용하는 경우에만 이러한 스케줄링 클래스 만이 priority 매개 변수에 둘 이상의 값을 허용하므로 실제로 사용하는 것이 의미가 있습니다. 설명을 보려면 stack overflow answer을 참조하십시오.

운좋게도 또는 유감스럽게도 그것은 원근법의 문제입니다. Linux POSIX 스레드 라이브러리 구현 (오래된 LinuxThread와 현재 NPTL 구현)은 모두 "좋은 가치"가 프로세스가 아니라는 점에서 완전히 POSIX와 호환되지 않습니다 특정하지만 스레드 특정 매개 변수, 그래서 당신은 setpriority(3) 리눅스에서 스레드의 niceness를 변경할 수있는 것 같습니다. 이 주장은 pthreads(7) 매뉴얼 페이지의 호환성 노트를 기반으로합니다 (해당 페이지에서 "nice value"로 검색). 나는 실제로 실제로 (직접적으로) 테스트하지 않았다.

스레드 친절 함을 변경하는 불완전한 POSIX 방법을 사용하기로 결정한 경우, 누군가가 언급 된 위반 사항을 수정하기로 결정할 숨어있는 가능성이 있음을 유의하십시오.이 경우 스레드를 변경할 방법이없는 것 같습니다. 정상 스케줄링 클래스 (SCHED_OTHER)를 사용하는 경우 Linux의 우선 순위.

0

MacOS 또는 iOS와 같은 BSD 기반 OS 솔루션을 검색하는 사용자는 필요한 경우 POSIX 대신 Mach를 사용하여 스레드의 우선 순위를 설정하는 것이 좋습니다.

#include <mach/mach_init.h> 
#include <mach/thread_policy.h> 
#include <mach/sched.h> 
#include <pthread.h> 

int set_realtime(int period, int computation, int constraint) { 
    struct thread_time_constraint_policy ttcpolicy; 
    int ret; 
    thread_port_t threadport = pthread_mach_thread_np(pthread_self()); 

    ttcpolicy.period=period; // HZ/160 
    ttcpolicy.computation=computation; // HZ/3300; 
    ttcpolicy.constraint=constraint; // HZ/2200; 
    ttcpolicy.preemptible=1; 

    if ((ret=thread_policy_set(threadport, 
     THREAD_TIME_CONSTRAINT_POLICY, (thread_policy_t)&ttcpolicy, 
     THREAD_TIME_CONSTRAINT_POLICY_COUNT)) != KERN_SUCCESS) { 
      fprintf(stderr, "set_realtime() failed.\n"); 
      return 0; 
    } 
    return 1; 
} 

출처 : 당신은 pthread_attr_getschedpolicy`에 초기화되지 않은`pthread_attr_t`에 대한 포인터를 전달할 수 없습니다 https://developer.apple.com/library/content/documentation/Darwin/Conceptual/KernelProgramming/scheduler/scheduler.html