int var; //全局变量 UINT ThreadFunction(LPVOIDpParam) { var = 0; while (var < MaxValue) { //线程处理 ::InterlockedIncrement(long*) &var); } return 0; } 请看下列程序: int globalFlag = false; DWORD WINAPI ThreadFunc(LPVOID n) { Sleep(2000); globalFlag = true; return 0; } int main() { HANDLE hThrd; DWORD threadId; hThrd = CreateThread(NULL, 0, ThreadFunc, NULL, 0, &threadId); if (hThrd) { printf("Thread launched\n"); CloseHandle(hThrd); } while (!globalFlag) ; printf("exit\n"); } |
HANDLE CreateEvent( LPSECURITY_ATTRIBUTES lpEventAttributes, // SECURITY_ATTRIBUTES结构指针,可为NULL BOOL bManualReset, // 手动/自动 // TRUE:在WaitForSingleObject后必须手动调用ResetEvent清除信号 // FALSE:在WaitForSingleObject后,系统自动清除事件信号 BOOL bInitialState, //初始状态 LPCTSTR lpName //事件的名称 ); |
HANDLE hEvent=OpenEvent(EVENT_ALL_ACCESS,true,"MyEvent"); ResetEvent(hEvent); |
CRITICAL_SECTION gCriticalSection; |
VOID WINAPI InitializeCriticalSection( LPCRITICAL_SECTION lpCriticalSection //指向程序员定义的CRITICAL_SECTION变量 ); |
VOID WINAPI DeleteCriticalSection( LPCRITICAL_SECTION lpCriticalSection //指向一个不再需要的CRITICAL_SECTION变量 ); |
VOID WINAPI EnterCriticalSection( LPCRITICAL_SECTION lpCriticalSection //指向一个你即将锁定的CRITICAL_SECTION变量 ); |
VOID WINAPI LeaveCriticalSection( LPCRITICAL_SECTION lpCriticalSection //指向一个你即将离开的CRITICAL_SECTION变量 ); |
void UpdateData() { EnterCriticalSection(&gCriticalSection); ...//do something LeaveCriticalSection(&gCriticalSection); } |
HANDLE CreateMutex( LPSECURITY_ATTRIBUTES lpMutexAttributes, // 安全属性结构指针,可为NULL BOOL bInitialOwner, //是否占有该互斥量,TRUE:占有,FALSE:不占有 LPCTSTR lpName //信号量的名称 ); |
HANDLE hMutex; hMutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, L"mutexName"); if (hMutex){ … } else{ … } |
BOOL WINAPI ReleaseMutex( HANDLE hMutex ); |
void UpdateResource() { WaitForSingleObject(hMutex,…); ...//do something ReleaseMutex(hMutex); } |
HANDLE CreateSemaphore ( PSECURITY_ATTRIBUTE psa, LONG lInitialCount, //开始时可供使用的资源数 LONG lMaximumCount, //最大资源数 PCTSTR pszName); |
BOOL WINAPI ReleaseSemaphore( HANDLE hSemaphore, LONG lReleaseCount, //信号量的当前资源数增加lReleaseCount LPLONG lpPreviousCount ); |
HANDLE OpenSemaphore ( DWORD fdwAccess, BOOL bInherithandle, PCTSTR pszName ); |
int globalVar = 0; DWORD WINAPI ThreadFunc1(LPVOID n) { globalVar++; return 0; } DWORD WINAPI ThreadFunc2(LPVOID n) { globalVar++; return 0; } |
00401038 mov eax,[globalVar (0042d3f0)] 0040103D add eax,1 00401040 mov [globalVar (0042d3f0)],eax |
int globalVar = 0; DWORD WINAPI ThreadFunc1(LPVOID n) { InterlockedExchangeAdd(&globalVar,1); return 0; } DWORD WINAPI ThreadFunc2(LPVOID n) { InterlockedExchangeAdd(&globalVar,1); return 0; } |
HANDLE CreateWaitableTimer( PSECURITY_ATTRISUTES psa, BOOL fManualReset,//人工重置或自动重置定时器 PCTSTR pszName); |
BOOL SetWaitableTimer( HANDLE hTimer, //要设置的定时器 const LARGE_INTEGER *pDueTime, //指明定时器第一次激活的时间 LONG lPeriod, //指明此后定时器应该间隔多长时间激活一次 PTIMERAPCROUTINE pfnCompletionRoutine, PVOID PvArgToCompletionRoutine, BOOL fResume); |
BOOl Cancel WaitableTimer( HANDLE hTimer //要取消的定时器 ); |
HANDLE OpenWaitableTimer ( DWORD fdwAccess, BOOL bInherithandle, PCTSTR pszName ); |
#include <windows.h> #include <stdio.h> CRITICAL_SECTION cs1, cs2; long WINAPI ThreadFn(long); main() { long iThreadID; InitializeCriticalSection(&cs1); InitializeCriticalSection(&cs2); CloseHandle(CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadFn, NULL, 0,&iThreadID)); while (TRUE) { EnterCriticalSection(&cs1); printf("\n线程1占用临界区1"); EnterCriticalSection(&cs2); printf("\n线程1占用临界区2"); printf("\n线程1占用两个临界区"); LeaveCriticalSection(&cs2); LeaveCriticalSection(&cs1); printf("\n线程1释放两个临界区"); Sleep(20); }; return (0); } long WINAPI ThreadFn(long lParam) { while (TRUE) { EnterCriticalSection(&cs2); printf("\n线程2占用临界区2"); EnterCriticalSection(&cs1); printf("\n线程2占用临界区1"); printf("\n线程2占用两个临界区"); LeaveCriticalSection(&cs1); LeaveCriticalSection(&cs2); printf("\n线程2释放两个临界区"); Sleep(20); }; } |
long WINAPI ThreadFn(long lParam) { while (TRUE) { EnterCriticalSection(&cs1); printf("\n线程2占用临界区1"); EnterCriticalSection(&cs2); printf("\n线程2占用临界区2"); printf("\n线程2占用两个临界区"); LeaveCriticalSection(&cs1); LeaveCriticalSection(&cs2); printf("\n线程2释放两个临界区"); Sleep(20); }; } |