win32 Threads

A thread is fairly simple: it is created, it executes code, and then it exits.

Threads are created as a result of another thread calling the CreateThread() function, unless the thread is the primary thread for the process.

Usually, the primary thread for the process starts immediately after the thread has been created. If the process is created by calling the CreateProcess() function, however, the primary thread can be created in the suspended state by specifying the CREATE_SUSPENDED flag.

The various thread creation functions take a pointer to a function as a place to begin executing the thread; this function is known as the thread entry procedure. The function pointer must be a pointer to a function of type THREAD_START_ROUTINE.

CreateThread() is the basic win32 thread creation function. This function creates a new thread within the process of the caller. The new thread has its own stack, its own copy of the machine’s registers, and will be scheduled independently by the OS.

The CreateThread() function takes six parameters. The first parameter can be specified as NULL; it is supposed to be a pointer to SECURITY_ATTRIBUTES structure, which describes the security and inheritance properties of the new thread. Putting NULL here will cause the thread to be created with default security and the thread handle will not be inheritable by child processes of the current process.

The second parameter to CreateThread() is a DWORD value indicating the size of the new thread’s stack. The system will automatically round this value to the nearest page. If the parameter is 0, it will be the default size for the process. The third parameter is the address of the user function that the new thread will call when it begins executing. The fourth argument is a pointer to a parameter value to be passed into the function. The fifth parameter is a DWORD value representing creation flags for the thread. The final parameter is a pointer to a DWORD variable that receives the thread ID.

#include "stdafx.h"
#include <Windows.h>

DWORD WINAPI threadCreate(LPVOID threadParam);
DWORD WINAPI threadCreateWithParam(LPVOID threadParam);

int _tmain(int argc, _TCHAR* argv[])
{
    DWORD dwThreadIDOne, dwThreadIDTwo;
    int iValue = 73;
    int iCounter = 0;

    CreateThread(NULL, NULL, &threadCreate, NULL, 0, &dwThreadIDOne);
    printf("Just created thread %d\n", dwThreadIDOne);

    Sleep(2000);

    CreateThread(NULL, NULL, &threadCreateWithParam, &iValue, 0, &dwThreadIDTwo);
    printf("Just created thread %d\n", dwThreadIDTwo);

    for (iCounter; iCounter < 4; iCounter++){
        printf("In main thread.\n");
        Sleep(500);
    }

    Sleep(3000);

    return 0;
}


DWORD WINAPI threadCreate(LPVOID threadParam){
    int i = 10;
    while (i-- > 0){
        printf("In the new thread.\n");
        Sleep(500);
    }

    //oddly named ERROR_SUCCESS
    //indicates everything is copacetic 
    return ERROR_SUCCESS;
}

DWORD WINAPI threadCreateWithParam(LPVOID threadParam){
    int *iParam = (int*)threadParam;
    int i = 5;
    while (i-- > 0){
        printf("In the thread that received %d.\n", *iParam);
        Sleep(500);
    }

    //aka return 0
    return ERROR_SUCCESS;
}

When threads are initially created, they are unaware of their identifier. From within the thread, we can acquire the unique numeric value assigned to the thread when it is created by calling the GetCurrentThreadId() function.

#include "stdafx.h"
#include <Windows.h>

DWORD WINAPI makeThread(LPVOID lpThreadParam);

int _tmain(int argc, _TCHAR* argv[])
{
    DWORD dwThreadID;

    CreateThread(NULL, NULL, &makeThread, NULL, NULL, &dwThreadID);

    printf("Created thread %d from main thread %d.\n", dwThreadID, GetCurrentThreadId());

    Sleep(1000);
    
    return 0;
}


DWORD WINAPI makeThread(LPVOID lpThreadParam){
    printf("In thread %d.\n", GetCurrentThreadId());

    return ERROR_SUCCESS;
}

We can also acquire a HANDLE to the current thread by calling the GetCurrentThread() function. Note that this HANDLE is actually a pseudo-handle.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s