Tuesday, February 5, 2008

Windows Threading

include windows.h
HANDLE CreateThread(LPSECURITY_ATTRIBUTES secAttr,
SIZE_T stackSize,
LPTHREAD_START_ROUTINE threadFunc,
LPVOID param,
DWORD flags,
LPDWORD threadID);

Thread can be destroyed by calling CloseHandle(HANDLE );
or BOOL TerminateThread(HANDLE thread, DWORD status); //no cleanup
VOID ExitThread(DWORD status); //cleanup stacks

The Visual C++ alternatives to CreateThread( ) and ExitThread( ) are _beginthreadex( ) and _endthreadex( ). Both require the header file .

DWORD SuspendThread(HANDLE hThread);
DWORD ResumeThread(HANDLE hThread);
DWORD GetPriorityClass(HANDLE hApp);
BOOL SetPriorityClass(HANDLE hApp, DWORD priority);
HANDLE GetCurrentThread(void); //get the main thread

Windows Synchronization:
- Semaphore
- Mutex //global object can be used by multi process
- Event Object
- Waitable Timer
- Critical section

MUTEX:
HANDLE CreateMutex(LPSECURITY_ATTRIBUTES secAttr, BOOL acquire, LPCSTR name);
name = NULL => localized sync
A mutex handle is automatically closed when the main process ends. You can explicitly close a mutex handle when it is no longer needed by calling CloseHandle( ).
DWORD WaitForSingleObject(HANDLE hObject, DWORD howLong);
BOOL ReleaseMutex(HANDLE hMutex);

if(WaitForSingleObject(hMutex, 10000)==WAIT_TIMEOUT) {
// handle time-out error
}

// access the resource

ReleaseMutex(hMutex);















No comments: