Chapter: Multicore Application Programming For Windows, Linux, and Oracle Solaris - Windows Threading

| Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail |

Setting Thread Priority

Windows uses a priority system to determine which thread gets the next slice of CPU resources.

Setting Thread Priority

 

Windows uses a priority system to determine which thread gets the next slice of CPU resources. The higher the priority of a thread, the more CPU time it will get, and con-versely a thread with a low priority will get fewer CPU resource than other higher-priority threads. In some instances, it can be useful to adjust the priority of different threads in an application. The obvious example is when the application is performing a long-running background task. A background task is best run at a low priority in order to keep the machine responsive, while a high-priority background task could consume all the CPU resources over a long period of time and stop the machine from performing other short-lived compute-intensive tasks.

 

Consider burning a CD or DVD. Here the system needs to keep feeding data to the burner, and any interruption to the data stream could cause an error resulting in an unusable disk being written. To avoid this, it might be appropriate to run the burning application at a higher than usual priority level.

 

The function that sets the priority of the thread is SetThreadPriority(), which takes a handle to the thread plus the desired priority level. There is a corresponding function GetThreadPriority() that takes a handle to a thread and returns the priority level of that thread.

 

Listing 6.40 shows code that manipulates the priority levels of two threads to ensure that one of the threads gets more CPU resources than the other. All threads are created with a priority level of THREAD_PRIORITY_NORMAL. The slow thread is created first and sets its own priority to be below normal. Then the fast thread is created, and it sets its own priority level to be above normal. The slow thread is created first in order to give it the chance to complete first. On a system with one core, the slow thread will get less CPU resources than the fast thread and will therefore complete later. On an idle multicore sys-tem, both threads will be scheduled simultaneously so they will complete at the same time.

 

Listing 6.40   Setting Thread Priority

#include <windows.h> #include <process.h>

 

unsigned int __stdcall fastthread( void * data )

 

{

 

double d=0.0;

 

printf( "Fast thread started\n" );

 

SetThreadPriority( GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL );

 

for (int i=0; i<100000000; i++)

 

{

 

d += d;

 

}

 

printf( "Fast thread finished\n" ); return 0;

 

}

 

unsigned int __stdcall slowthread( void * data )

 

{

 

double d=0.0;

 

printf( "Slow thread started\n" );

 

SetThreadPriority( GetCurrentThread(), THREAD_PRIORITY_BELOW_NORMAL );

 

for (int i=0; i<100000000; i++)

 

{

 

d += d;

 

}

printf( "Slow thread finished\n" ); return 0;

 

}

 

int _tmain( int argc, _TCHAR* argv[] )

 

{

 

HANDLE hfast,hslow;

 

hslow = (HANDLE)_beginthreadex( 0, 0, &slowthread, 0, 0, 0 ); hfast = (HANDLE)_beginthreadex( 0, 0, &fastthread, 0, 0, 0 ); WaitForSingleObject( hfast, INFINITE );

 

WaitForSingleObject( hslow, INFINITE ); getchar();

 

return 0;

 

}

One of the issues caused by adjusting the priority of threads (or processes) is priority inversion, which occurs when a higher-priority thread ends up waiting for a lower-priority thread to complete some task. The classic example of this is when a lower-priority thread enters a critical region but because of its priority ends up taking a long time to exit the critical region. While this is happening, a higher-priority thread can be waiting to enter the critical region.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.