CreateThread ( LPSECURITY_ATTRIBUTES lpThreadAttributes, SIZE_T dwStackSize, LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, DWORD dwCreationFlags, LPDWPRD lpThreadId);----------------------------------------------------------------------------------// 互斥量、 (同一时间只获得一个线程)创建互斥量对象:HANDLE WINAPI CteateMutex( LPSECURITY_ATTRIBUTES lpMutexAttributes, // 安全特征 BOOL bInitialOwner, // 所有权 LPCWSTR szName // 互斥量名称);打开一个现有的互斥量:HANDLE WINAPI OpenMutex( DWORD dwDesiredAccess, BOOL bInheritHandle, LPCTSTR szName);释放互斥量:BOOL WINAPI ReleaseMutex( HANDLE hMutex);----------------------------------------------------------------------------------// 信号量、 (同一时间允许一定数量的线程通过) 创建信号量对象:HANDLE WINAPI CreateSemaphore( LPSECURITY_ATTRIBUTES lpSemaphpreAttributes, LONG lInitialCount, // 设置初始数目以确定信号量的初始触发状态 LONG lMaxinumCount, // 可同时持有信号量所有权的最多线程对象数目 LPCWSTR lpName);打开一个信号量对象:HANDLE WINAPI OpenSemaphore( DWORD dwDesiredAccess, BOOL bInheritHandle, LPCSTR lpName );释放信号量对象:BOOL WINAPI ReleaseSemaphore( HANDLE hSemaphore, LONG lReleaseCount, LPLONG lpPreviousCount );// 事件、 (播报任何线程都能接收到的公共信号)创建事件对象:HANDLE WINAPI CreateEvent( LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset, BOOL bInitialState, // 事件初始状态 LPCSTR lpName // 事件名 );打开事件对象:HANDLE WINAPI OpenEvent( DWORD dwDesiredAccess, BOOL bInheritHandle, LPCSTR lpName );设置事件对象:BOOL WINAPI SetEvent( __in HANDLE hEvent );重置事件对象:BOOL WINAPI ResetEvent( __in HANDLE hEvent );// 脉冲一个事件对象:BOOL WINAPI PulseEvent( __in HANDLE hEvent );-----------------------------------------------------------------------------------// 临界区、 (智能在一个进程中使用) 声明临界区对象:CRITICAL_SECTION m_CritSec;// 初始化临界区对象VOID WINAPI InitializeCriticalSection( LPCRITICAL_SECTION lpCriticalSection );// 线程进入临界区:VOID WINAPI EnterCriticalSection( LPCRITICAL_SECTION lpCriticalSection );BOOL WINAPI TryEnterCriticalSection( __inout LPCRITICAL_SECTION lpCriticalSection );// 线程离开临界区:VOID WINAPI LeaveCriticalSection( LPCRITICAL_SECTION lpCriticalSection );// 释放临界区资源:VOID WINAPI DeleteCriticalSection( LPCRITICAL_SECTION lpCriticalSection );------------------------------------------------------------------------------------
线程同步的四种发法// 实例int i = 0;HANDLE hMutex; // 互斥量对象
HANDLE hSemaphore; // 信号量句柄
HANDLE hEvent; // 事件句柄
CRITICAL_SECTION Critical; //定义临界区句柄
DWORD WINAPI ThreadPrco1(LPVOID){ while (i < 100) { EnterCriticalSection(&Critical); //WaitForSingleObject(/*hMutex*/ /*hSemaphore*/ hEvent, INFINITE); cout<<"ThreadPrco1 "<<i<<endl; ++i; Sleep(10); // ReleaseMutex(hMutex); // ReleaseSemaphore(hSemaphore, 1, 0); // SetEvent(hEvent); LeaveCriticalSection(&Critical); } return 0L;}
DWORD WINAPI ThreadPrco2(LPVOID){ while (i < 100) { EnterCriticalSection(&Critical); WaitForSingleObject(/*hMutex*/ /*hSemaphore*/ hEvent, INFINITE); cout<<"ThreadPrco2 "<<i<<endl; ++i; Sleep(10); // ReleaseMutex(hMutex); // ReleaseSemaphore(hSemaphore, 1, 0); // SetEvent(hEvent); LeaveCriticalSection(&Critical); } return 0L;}
int main(int argc, _TCHAR* argv[]){ // hMutex = CreateMutex(NULL, FALSE, L"mutex");
// hSemaphore = CreateSemaphore(NULL, 1, 100, L"sema");
// hEvent = CreateEvent(NULL, FALSE, TRUE, L"event");
InitializeCriticalSection(&Critical); // 初始化临界区对象
CreateThread(NULL, 0, ThreadPrco1, NULL, 0, NULL);
CreateThread(NULL, 0, ThreadPrco2, NULL, 0, NULL);
Sleep(2*1000); system("pause"); return 0;}----------------------------------------------------------------------------------------
转载于:https://www.cnblogs.com/shuang0109/p/9828977.html
相关资源:POSIX Threads (pthreads) for Win32