Windows Thread Synchronization 47

 

 

 

 

 

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.

 

Thread Scheduling Program Example: Creating Win32 C++ console application project

 

Then, add the source file and give it a suitable name.

 

Thread Scheduling Program Example: Adding new C++ source file

 

Next, add the following source code.

 

#include <Windows.h>

#include <stdio.h>

 

DWORD WINAPI thread_function(LPVOID arg);

WCHAR message[] = L"Hello I'm a Thread";

DWORD thread_finished = 0;

 

int wmain()

{

      int count=0;

      HANDLE a_thread;

 

      wprintf(L"The main() process ID is %u\n", GetCurrentProcessId());

      wprintf(L"The 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(L"CreateThread() - Thread creation failed, error %u\n", GetLastError());

            exit(EXIT_FAILURE);

      }

      else

            wprintf(L"CreateThread() is OK!\n");

 

      if (!SetThreadPriority(a_thread, THREAD_PRIORITY_LOWEST))

      {

            wprintf(L"SetThreadPriority(..., THREAD_PRIORITY_LOWEST) - Setting schedule priority failed, error %u\n", GetLastError());

            exit(EXIT_FAILURE);

      }

      else

            wprintf(L"SetThreadPriority(...,THREAD_PRIORITY_LOWEST) is OK!\n");

     

      if(CloseHandle(a_thread) != 0)

            wprintf(L"a_thread handle was successfully closed!\n");

      else

            wprintf(L"Failed to close a_thread handle, error %u\n", GetLastError());

 

      while(!thread_finished)

      {

            wprintf(L"Waiting for thread to finished count %d\n", ++count);

            Sleep(1000);

      }

     

      wprintf(L"Well, the other thread finished, bye!\n");

      exit(EXIT_SUCCESS);

}

 

DWORD WINAPI thread_function(LPVOID arg)

{

      wprintf(L"thread_function() is running. Arg received was %s, thread ID is %u\n", arg, GetCurrentThreadId());

      Sleep(4000);

      wprintf(L"Second 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.

 

Thread Scheduling Program Example: A  sample program output in action

 

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.

 

 

 

 

< Thread Synchronization 46 | Thread Synchronization Programming | Win32 Programming | Thread Synchronization 48 >