Programming tips

c++ thread programming

Thread body

DWORD WINAPI ThreadFunc (PVOID pvParam)
{
DWORD dwResult = 0;

......
return (dwResult);

}

Start and stop a thread

DWORD dwThreadID;
HANDLE hThread = CreateThread( NULL, 0, ThreadFunc, this, &dwThreadID);
GetExitCodeThread(hThread, &dwThreadID);
CloseHandle(hThread);

Look into the CreateThread function

HANDLE CreateThread(
PSECURITY_ATTRIBUTES psa, //Security option of thread kernel object. Default: 'NULL'
DWORD cbStackSize, //Size of the thread stack. Default: '0'
PTHREAD_START_ROUTINE pfnStartAddr, //Name of the thread function.
PVOID pvParam, //Argument list
DWORD dwCreateFlags, //Thread control flag
PDWORD pdwThreadID); //Thread ID

How to terminate a thread?

1. Return a value inside the thread function body.
2. Call Exitthread function inside the thread function body.
3. Call TerminateThread() in same process or another process
4. Terminate the process which embraces the target thread

No comments: