Windows Scheduling Program Example
In following example, the thread priority level is set to the lowest level within the given policy or class. When using the Windows API, it is accomplished by a call to SetThreadPriority().
Thread Scheduling Program Example
Create a new empty Win32 console application project. Give a suitable project name and change the project location if needed.
Then, add the source file and give it a suitable name.
Next, add the following source code.
#include <Windows.h>
#include <stdio.h>
DWORD WINAPI thread_function(LPVOID arg);
WCHAR message[] = LHello I'm a Thread;
DWORD thread_finished = 0;
int wmain()
{
int count=0;
HANDLE a_thread;
wprintf(LThe main() process ID is %u\n, GetCurrentProcessId());
wprintf(LThe main() thread ID is %u\n, GetCurrentThreadId());
wprintf(L\n);
// Create a new thread.
a_thread = CreateThread(NULL, 0, thread_function, (LPVOID)message, 0, NULL);
if (a_thread == NULL)
{
wprintf(LCreateThread() - Thread creation failed, error %u\n, GetLastError());
exit(EXIT_FAILURE);
}
else
wprintf(LCreateThread() is OK!\n);
if (!SetThreadPriority(a_thread, THREAD_PRIORITY_LOWEST))
{
wprintf(LSetThreadPriority(..., THREAD_PRIORITY_LOWEST) - Setting schedule priority failed, error %u\n, GetLastError());
exit(EXIT_FAILURE);
}
else
wprintf(LSetThreadPriority(...,THREAD_PRIORITY_LOWEST) is OK!\n);
if(CloseHandle(a_thread) != 0)
wprintf(La_thread handle was successfully closed!\n);
else
wprintf(LFailed to close a_thread handle, error %u\n, GetLastError());
while(!thread_finished)
{
wprintf(LWaiting for thread to finished count %d\n, ++count);
Sleep(1000);
}
wprintf(LWell, the other thread finished, bye!\n);
exit(EXIT_SUCCESS);
}
DWORD WINAPI thread_function(LPVOID arg)
{
wprintf(Lthread_function() is running. Arg received was %s, thread ID is %u\n, arg, GetCurrentThreadId());
Sleep(4000);
wprintf(LSecond thread, ID %u setting finished flag, and exiting now...\n, GetCurrentThreadId());
thread_finished = 1;
return 100;
}
Build and run the project. The following screenshot is a sample output.
In the preceding Windows example, the priority level of the thread is adjusted to the lowest level within the priority class of the owning process. If you want to change the priority class as well as the priority level, insert the following code just before the SetThreadPriority call:
SetPriorityClass(GetCurrentProcess(), PriorityClass);
Where, PriorityClass will be one of the values shown in Table Y. Table Y summarizes how to change the scheduling priority for a thread and priority class for the owning process.
Table Y: PriorityClass Values
PriorityClass |
Meaning |
ABOVE_NORMAL_PRIORITY_CLASS |
Windows Server™ 2003 and Windows® XP: Indicates a process that has priority above NORMAL_PRIORITY_CLASS but below HIGH_PRIORITY_CLASS. |
BELOW_NORMAL_PRIORITY_CLASS |
Windows Server 2003 and Windows XP: Indicates a process that has priority above IDLE_PRIORITY_CLASS but below NORMAL_PRIORITY_CLASS. |
HIGH_PRIORITY_CLASS |
Specify this class for a process that performs time-critical tasks that must be executed immediately. The threads of the process preempt the threads of normal or idle priority-class processes. An example is the Task List, which must respond quickly when called by the user, regardless of the load on the operating system. Use extreme care when using the high-priority class because a high-priority class application can use nearly all available CPU time. |
IDLE_PRIORITY_CLASS |
Specify this class for a process whose threads run only when the system is idle. The threads of the process are preempted by the threads of any process running in a higher priority class. An example is a screen saver. The idle-priority class is inherited by child processes. |
NORMAL_PRIORITY_CLASS |
Specify this class for a process with no special scheduling needs. |
REALTIME_PRIORITY_CLASS |
Specify this class for a process that has the highest possible priority. The threads of the process preempt the threads of all other processes, including the operating system processes, which may be performing important tasks. For example, a real-time process that executes for more than a very brief interval can prevent disk caches from flushing or can cause the mouse to be unresponsive. |