Windows Thread Synchronization 39

 

 

 

 

 

Cancelling a Thread Program Example

 

Create a new empty Win32 console application project. Give a suitable project name and change the project location if needed.

 

Cancelling a Thread Program Example: Creating new Win32 C++ console application project

 

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

 

Cancelling a Thread 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)

{

      wprintf(L"thread_function() is running. Argument received was \"%s\"\n", arg);

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

      {

            wprintf(L"Thread is running pass #%d, ID %u...\n", i, GetCurrentThreadId());

            // Give enough time...

            Sleep(1000);

      }

      return 100;

}

 

void wmain()

{

      HANDLE a_thread;

      DWORD thread_result, dwThreadId;

      WCHAR message[] = L"Hello World";

 

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

      wprintf(L"Current thread (main()) ID is %u\n", GetCurrentThreadId());

 

      wprintf(L"\n");

     

      // Create a new thread.

      a_thread = CreateThread(NULL, 0, thread_function, (LPVOID)message, 0, &dwThreadId);

      if (a_thread == NULL)

      {

            wprintf(L"CreateThread() failed, error %u", GetLastError());

            exit(EXIT_FAILURE);

      }

      else

            wprintf(L"CreateThread() is OK! Thread ID is %u\n", dwThreadId);

      // Give enough time, else deadlock

      Sleep(3000);

     

      // If the function succeeds, the return value is nonzero.

      // If the function fails, the return value is zero.

      if (!TerminateThread(a_thread, 0))

      {

            wprintf(L"TerminateThread() - Thread cancellation failed! error %u\n", GetLastError());

            exit(EXIT_FAILURE);

      }

      else

            wprintf(L"TerminateThread() - Cancelling thread %u\n", dwThreadId);

     

      wprintf(L"\n");

 

      wprintf(L"Waiting for thread %u to finish...\n", dwThreadId);

      if(WaitForSingleObject(a_thread, INFINITE) != WAIT_OBJECT_0)

      {

            wprintf(L"WaitForSingleObject(), failed, error %u\n", GetLastError());

            exit(EXIT_FAILURE);

      }

      else

            wprintf(L"WaitForSingleObject()is OK! Some event should be signaled!\n");

 

      wprintf(L"\n");

 

      // If the function succeeds, the return value is nonzero.

      // If the function fails, the return value is zero.

      if(GetExitCodeThread(a_thread, &thread_result) != 0)

            wprintf(L"GetExitCodeThread() is OK!, Exit code %u\n", thread_result);

      else

            wprintf(L"GetExitCodeThread() failed, error %u\n", GetLastError());

 

      if(CloseHandle(a_thread) != 0)

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

      else

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

 

      exit(EXIT_SUCCESS);

}

 

Build and run the project. The following screenshot is a sample output.

Cancelling a Thread Program Example: A sample console program output

 

The execution may be hanging. Can you find the reason?

 

Cancelling a Thread Program Example: A sample program output that hanging

 

When verifying the condition using Windows Task Manager, we can see a single thread is still running.

 

Cancelling a Thread Program Example: Task Manager shows a thread still in action

 

 

 

 

< Thread Synchronization 38 | Thread Synchronization Programming | Win32 Programming | Thread Synchronization 40 >