How to Set thread priority in Linux
We use int pthread_create (pthread_t * thread, const pthread_attr_t * ATTR, void * (* start_routine) (void *), void * Arg );
But how to set the thread priority?
When discussing this issue, we first need to determine the scheduling policy used by the current thread. POSIX provides
Int pthread_attr_getschedpolicy (const pthread_attr_t * ATTR, int * policy); function to obtain
The scheduling policies used are sched_fifo, sched_rr, and sched_other.
We can use
Int sched_get_priority_max (INT policy );
Int sched_get_priority_min (INT policy );
To obtain the maximum and minimum priority values set by the thread. If the call is successful, the maximum and minimum priority values are returned. Otherwise,-1 is returned.
In my current Linux system, I used the following programs (see the appendix for programs) to obtain the maximum and minimum priorities of the three scheduling policies:
Policy = sched_other
Show current configuration of priority
Max_priority = 0
Min_priority = 0
Show sched_fifo of priority
Max_priority = 99
Min_priority = 1
Show sched_rr of priority
Max_priority = 99
Min_priority = 1
Show priority of current thread
Priority = 0
Set thread Policy
Set sched_fifo Policy
Policy = sched_fifo
Set sched_rr Policy
Policy = sched_rr
Restore current policy
Policy = sched_other
We can see that
Sched_other does not support priority, while sched_fifo and sched_rr support priority. They are 1 and 99, respectively. A larger value indicates a higher priority. From the above results, we can see that if the program controls the priority of the thread, it generally uses pthread_attr_getschedpolicy to obtain the scheduling policy used by the system. If it is sched_other, indicates that the current policy does not support the use of thread priority; otherwise, yes. Of course, the priority range must be between the maximum and minimum values. We can get it through sched_get_priority_max and sched_get_priority_min.
Some netizens may ask, can we use int pthread_attr_setschedpolicy (pthread_attr_t * ATTR, int Policy) to set the scheduling policy we need? I think it is completely feasible (some systems need to define _ posix_thread_priority_scheduling) as long as the system implements the corresponding call policy.
After talking about it for half a day, we haven't said how to set the priority level when the system permits the use of thread priority?
Int pthread_attr_setschedparam (pthread_attr_t * ATTR, const struct sched_param * PARAM );
Int pthread_attr_getschedparam (const pthread_attr_t * ATTR, struct sched_param * PARAM );
The preceding two functions are used to set the thread priority. The definition of struct sched_param is as follows:
Struct sched_param
{
Int _ sched_priority; // specifies the thread priority.
};
For example, create a thread with a priority of 10.
Pthread_attr_t ATTR;
Struct sched_param Param;
Pthread_attr_init (& ATTR );
Pthread_attr_setschedpolicy (& ATTR, sched_rr );
Param. sched_priority = 10;
Pthread_attr_setschedparam (& ATTR, & PARAM );
Pthread_create (XXX, & ATTR, XXX, XXX );
Pthread_attr_destroy (& ATTR );
Appendix: Test procedure used:
# Include <iostream>
# Include <pthread. h>
# Include <sched. h>
# Include <assert. h>
Using namespace STD;
Static int get_thread_policy (pthread_attr_t & ATTR)
{
Int policy;
Int rs = pthread_attr_getschedpolicy (& ATTR, & Policy );
Assert (rs = 0 );
Switch (Policy)
{
Case sched_fifo:
Cout <"policy = sched_fifo" <Endl;
Break;
Case sched_rr:
Cout <"policy = sched_rr" <Endl;
Break;
Case sched_other:
Cout <"policy = sched_other" <Endl;
Break;
Default:
Cout <"policy = unknown" <Endl;
Break;
}
Return Policy;
}
Static void show_thread_priority (pthread_attr_t & ATTR, int Policy)
{
Int priority = sched_get_priority_max (policy );
Assert (priority! =-1 );
Cout <"max_priority =" <priority <Endl;
Priority = sched_get_priority_min (policy );
Assert (priority! =-1 );
Cout <"min_priority =" <priority <Endl;
}
Static int get_thread_priority (pthread_attr_t & ATTR)
{
Struct sched_param Param;
Int rs = pthread_attr_getschedparam (& ATTR, & PARAM );
Assert (rs = 0 );
Cout <"priority =" <Param. _ sched_priority <Endl;
Return Param. _ sched_priority;
}
Static void set_thread_policy (pthread_attr_t & ATTR, int Policy)
{
Int rs = pthread_attr_setschedpolicy (& ATTR, policy );
Assert (rs = 0 );
Get_thread_policy (ATTR );
}
Int main (void)
{
Pthread_attr_t ATTR;
Struct sched_param sched;
Int RS;
Rs = pthread_attr_init (& ATTR );
Assert (rs = 0 );
Int policy = get_thread_policy (ATTR );
Cout <"show current configuration of priority" <Endl;
Show_thread_priority (ATTR, policy );
Cout <"show sched_fifo of priority" <Endl;
Show_thread_priority (ATTR, sched_fifo );
Cout <"show sched_rr of priority" <Endl;
Show_thread_priority (ATTR, sched_rr );
Cout <"show priority of current thread" <Endl;
Int priority = get_thread_priority (ATTR );
Cout <"set thread policy" <Endl;
Cout <"set sched_fifo policy" <Endl;
Set_thread_policy (ATTR, sched_fifo );
Cout <"set sched_rr policy" <Endl;
Set_thread_policy (ATTR, sched_rr );
Cout <"restore current policy" <Endl;
Set_thread_policy (ATTR, policy );
Rs = pthread_attr_destroy (& ATTR );
Assert (rs = 0 );
Return 0;
}
Compile command:
# G ++ pthread_priority3.c-O pthread_priority3-lpthread
Http://blog.csdn.net/vividonly/article/details/6902575