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 ) 退出临界区,相当于申请解锁
写个程序跑一下
[cpp] view plain copy
- #include <iostream>
- #include <process.h>
- #include <windows.h>
- using namespace std;
- CRITICAL_SECTION g_cs;
- int sum;
- unsigned int __stdcall ThreadFunc(void *arg)
- {
- int num = (int)arg;
- for(int i = 0; i < 8; i++)
- {
- EnterCriticalSection(&g_cs);
- sum++;
- cout<<"thread"<<num<<" sum is "<<sum<<endl;
- Sleep(10);
- LeaveCriticalSection(&g_cs);
- }
- return 0;
- }
- int main(void)
- {
- HANDLE handle[2];
- InitializeCriticalSection(&g_cs); //
- handle[0] = (HANDLE)_beginthreadex(NULL, 0, ThreadFunc, (void*)1, 0, NULL);
- handle[1] = (HANDLE)_beginthreadex(NULL, 0, ThreadFunc, (void*)2, 0, NULL);
- WaitForMultipleObjects(2, handle, TRUE, INFINITE);
- CloseHandle(handle[0]);
- CloseHandle(handle[1]);
- DeleteCriticalSection(&g_cs);
- return 0;
- }
在这里创建多线程用的是_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方式。