One-day training of Windows APIs (49) setthreadpriority and getthreadpriority Functions
Windows is the operating system for preemptive task execution. Both processes and threads have a priority to select the execution mode, which makes it easier for users to process multiple tasks. For example, when you listen to music while surfing the Internet, you can execute music tasks to a higher level, so that music does not sound intermittent. When you write a network program, a thread receives data from the network, and a thread writes data to the hard disk, you can set the priority of the network receiving thread to a higher level, this is because you can write the received data to the memory, cache it, and write it to the hard disk. Another type of task is to set the thread priority when the program you write needs to be executed in idle time. In this way, the setthreadpriority and getthreadpriority functions are used to set the thread priority and obtain the thread priority.
The setthreadpriority and getthreadpriority functions are declared as follows:
Bool
Winapi
Setthreadpriority (
_ In handle hthread,
_ In int npriority
);
Hthread is the thread handle.
Npriority is the priority of the thread.
Winbaseapi
Int
Winapi
Getthreadpriority (
_ In handle hthread
);
Hthread is the thread handle.
The returned value is the priority of the thread.
An example of calling a function is as follows:
#001 // set the thread priority and obtain the thread priority.
#002 // Cai junsheng 2007/10/11 QQ: 9073204 Shenzhen
#003 void threadpriority (void)
#004 {
#005 //
#006: setthreadpriority (m_hthread, thread_priority_highest );
#007
#008 //
#009 if (: getthreadpriority (m_hthread) = thread_priority_highest)
#010 {
#011 //
#012 outputdebugstring (_ T ("thread_priority_highest/R/N "));
#013}
#014
#015}