win32多线程锁之临界区

Win32的多线程锁主要有四种

临界区:critical_section

互斥:mutex

信号:semophore

事件:event

其中临界区不能跨进程,互斥,信号,事件属于内核对象,都可以跨进程


跟临界区相关的API

VOIDInitializeCriticalSection(LPCRITICAL_SECTION lpCriticalSection ) 创建临界区


VOID DeleteCriticalSection(LPCRITICAL_SECTIONlpCriticalSection ) 删除临界区


进入临界区,有两个函数

VOIDEnterCriticalSection(LPCRITICAL_SECTION lpCriticalSection )      相当于申请加锁,如果该临界区正被其他线程使用则该函数会等待到其他线程释放

BOOL TryEnterCriticalSection(LPCRITICAL_SECTIONlpCriticalSection )相当于申请加锁,和EnterCriticalSection不同如果该临界区正被其他线程使用则该函数会立即返回      FALSE,而不会等待

VOID LeaveCriticalSection(LPCRITICAL_SECTIONlpCriticalSection )    退出临界区,相当于申请解锁


写个程序跑一下

  1. #include <iostream>  
  2. #include <process.h>  
  3. #include <windows.h>  
  4. using namespace std;  
  5.   
  6. CRITICAL_SECTION g_cs;  
  7.   
  8. int sum;  
  9.   
  10. unsigned int __stdcall ThreadFunc(void *arg)  
  11. {  
  12.     int num = (int)arg;  
  13.     for(int i = 0; i < 8; i++)  
  14.     {  
  15.         EnterCriticalSection(&g_cs);  
  16.         sum++;  
  17.         cout<<"thread"<<num<<" sum is "<<sum<<endl;  
  18.         Sleep(10);  
  19.         LeaveCriticalSection(&g_cs);  
  20.     }  
  21.   
  22.     return 0;  
  23. }  
  24.   
  25. int main(void)  
  26. {  
  27.     HANDLE handle[2];  
  28.   
  29.     InitializeCriticalSection(&g_cs);   //  
  30.   
  31.     handle[0] = (HANDLE)_beginthreadex(NULL, 0, ThreadFunc, (void*)1, 0, NULL);  
  32.     handle[1] = (HANDLE)_beginthreadex(NULL, 0, ThreadFunc, (void*)2, 0, NULL);  
  33.   
  34.     WaitForMultipleObjects(2, handle, TRUE, INFINITE);  
  35.     CloseHandle(handle[0]);  
  36.     CloseHandle(handle[1]);  
  37.   
  38.     DeleteCriticalSection(&g_cs);  
  39.     return 0;  
  40. }  


在这里创建多线程用的是_beginthreadex,并没有使用win32的api的CreateThread函数,事实上不建议使用CreateThread函数,涉及到c语言函数的重入问题。在此或者使用_beginthread函数,不过_beginthreadex函数跟MFC的函数AfxBeginThread的参数类似。


_beginthreadex和_beginthread函数有一些不同,具体的参照MSDN,需要注意的是_beginthread和_beginthreadex,在线程函数正常结束后都会自动调用_endthread和_endthreadex函数,_endthread会close掉线程的handle,_endthreadex则不会。线程函数的调用方式也有不同,_beginthread是_cdecl方式,_beginthreadex是_stdcall方式。




http://blog.csdn.net/simeone18/article/details/7002280

原文地址:https://www.cnblogs.com/xieyuan/p/3787436.html