In Linux, we can 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 (intPolicy);
Int sched_get_priority_min (intPolicy);
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 program 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_OTHERPriority is not supported, whileSched_fifo and sched_rr support priority, which are 1 and 99, respectively,
Greater valueThe higher the priority. From the above results, we can see that if the program controls the priority of the thread
Pthread_attr_getschedpolicy to obtain the scheduling policy used by the system. If it is sched_other, it indicates the current policy.
The use of thread priority is not supported; otherwise, yes. Of course, the priority range must be between the maximum and minimum values. We can use
sched_get_priority_maxAndSched_get_priority_min.
Some netizens may ask if we can pass
Int pthread_attr_setschedpolicy (pthread_attr_t *ATTR, IntPolicy); To set what you need
What about scheduling policies? I think it is all right (some systems need to be defined_ Posix_thread_priority_scheduling), as long
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.
};
Test program 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;
}