Equivalent of SetThreadPriority on Linux (pthreads)

前端 未结 5 1661
清歌不尽
清歌不尽 2020-12-23 12:24

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.

#inc         


        
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-23 13:00

    For those who may be searching for BSD based OS solutions such as MacOS or iOS, you may want to consider setting the thread's priority using mach instead of the POSIX equivalent if necessary.

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

    Source: https://developer.apple.com/library/content/documentation/Darwin/Conceptual/KernelProgramming/scheduler/scheduler.html

提交回复
热议问题