Tuesday, April 10, 2012

Multithreading using CreateThread function in C++


        Multithreading is a specialized form of multitasking. In general, there are two types of multitasking: process-based and thread-based. A process is, in essence, a program that is executing. Thus, process-based multitasking is the feature that allows your computer to run two or more programs concurrently.

Creates a thread to execute within the virtual address space of the calling process.
To create a thread that runs in the virtual address space of another process, use the CreateRemoteThread function.

Syntax

HANDLE WINAPI CreateThread(
  __in_opt   LPSECURITY_ATTRIBUTES lpThreadAttributes,
  __in       SIZE_T dwStackSize,
  __in       LPTHREAD_START_ROUTINE lpStartAddress,
  __in_opt   LPVOID lpParameter,
  __in       DWORD dwCreationFlags,
  __out_opt  LPDWORD lpThreadId
);

Parameters

lpThreadAttributes [in, optional]
A pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. If lpThreadAttributes is NULL, the handle cannot be inherited. The lpSecurityDescriptor member of the structure specifies a security descriptor for the new thread. If lpThreadAttributes is NULL, the thread gets a default security descriptor.
dwStackSize [in]
The initial size of the stack, in bytes. The system rounds this value to the nearest page. If this parameter is zero, the new thread uses the default size for the executable. For more information, see Thread Stack Size.
lpStartAddress [in]
A pointer to the application-defined function to be executed by the thread. This pointer represents the starting address of the thread. For more information on the thread function, see ThreadProc.
lpParameter [in, optional]
A pointer to a variable to be passed to the thread.
dwCreationFlags [in]
The flags that control the creation of the thread. 

Return value

If the function succeeds, the return value is a handle to the new thread. If the function fails, the return value is NULL.

 Example

// MultiThread_App.cpp : main project file.

#include "stdafx.h"
#include "windows.h"
#include "strsafe.h"
#include "stdio.h"

using namespace System;

// Thread Function 1
DWORD WINAPI Thread_no_1( LPVOID lpParam )
{
    Console::WriteLine(L"Thread_no_1");
      return 0;
}

// Thread Function 2
DWORD WINAPI Thread_no_2( LPVOID lpParam )
{
    Console::WriteLine(L"Thread_no_2");
      return 0;
}

int main()
{
    int Data_Of_Thread_1 = 1;            // Data of Thread 1
    int Data_Of_Thread_2 = 2;            // Data of Thread 2

    HANDLE Handle_Of_Thread_1 = 0;       // variable to hold handle of Thread 1
    HANDLE Handle_Of_Thread_2 = 0;       // variable to hold handle of Thread 1
 
     HANDLE Array_Of_Thread_Handles[2];   // Aray to store thread handles

    printf("All threads are started \n");
     // Create thread 1.
    Handle_Of_Thread_1 = CreateThread( NULL, 0, Thread_no_1, &Data_Of_Thread_1, 0, NULL);
    if ( Handle_Of_Thread_1 == NULL)  ExitProcess(Data_Of_Thread_1);
 
    // Create thread 2.
    Handle_Of_Thread_2 = CreateThread( NULL, 0, Thread_no_2, &Data_Of_Thread_2, 0, NULL);
    if ( Handle_Of_Thread_2 == NULL)  ExitProcess(Data_Of_Thread_2);

    Array_Of_Thread_Handles[0] = Handle_Of_Thread_1;
    Array_Of_Thread_Handles[1] = Handle_Of_Thread_2;

    // Blocks/waits till all child threads are finished
    WaitForMultipleObjects( 2, Array_Of_Thread_Handles, TRUE, INFINITE);

    printf("Since All threads executed lets close their handles \n");

    // Close all thread handles upon completion.
    CloseHandle(Handle_Of_Thread_1);
    CloseHandle(Handle_Of_Thread_2);
    getchar();
    return 0;
}

No comments:

Post a Comment