Windows Thread Synchronization 38

 

 

 

 

 

Extra Synchronization Related working Program Examples

 

In Windows, threads are created using the CreateThread() function, which requires:

 

  1. The stack size of the thread.
  2. The security attributes of the thread.
  3. The address at which to begin execution of a procedure.
  4. A pointer to a variable to be passed to the thread.
  5. Flags that control the creation of the thread.
  6. An address to store the system-wide unique thread identifier.

 

After a thread is created, the thread identifier can be used to manage the thread (like get and set the priority of thread) until it has terminated. The next example demonstrates how you should use the CreateThread() function to create a single thread.

 

Creating a Single Thread Program Example

 

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

 

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

 

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

 

Creating a Single Thread Program Example: Adding new C++ source file

 

Next, add the following source code.

 

#include <Windows.h>

#include <stdio.h>

 

WCHAR message[] = L"Hello World";

 

DWORD WINAPI thread_function(LPVOID arg)

{

      wprintf(L"thread_function() has started. Sent argument was \"%s\"\n", arg);

      // Simulate on doing some job...

      Sleep(3000);

      // Copy a string to the global variable of the current process

      wprintf(L"Thread %u is modifying the global variable Message...\n", GetCurrentThreadId());

      wcscpy_s(message, sizeof(message), L"Bye!");

      // Return with an integer for verification

      return 100;

}

 

void wmain()

{

      HANDLE a_thread;

      DWORD a_threadId;

      DWORD thread_result;

 

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

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

      wprintf(L"Global variable of the current process, Message is now \"%s\"\n", message);

      wprintf(L"\n");

     

      // Create a new thread.

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

      if (a_thread == NULL)

      {

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

            exit(EXIT_FAILURE);

      }

      else

      {

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

            wprintf(L"CreateThread() is  OK, thread ID is %u\n", a_threadId);

      }

     

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

      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"Current thread ID is %u\n", GetCurrentThreadId());

      wprintf(L"\n");

 

      // Retrieves the termination status of the specified thread.

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

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

      GetExitCodeThread(a_thread, &thread_result);

      wprintf(L"Thread joined, it returned %d\n", thread_result);

      wprintf(L"Global variable of the current process, Message\n"

            L"modified by thread %u is now \"%s\"\n", a_threadId, message);

 

      wprintf(L"\n");

 

      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.

 

Creating a Single Thread Program Example:  A sample console program output

 

 

 

 

< Thread Synchronization 37 | Thread Synchronization Programming | Win32 Programming | Thread Synchronization 39 >