Skip to content

Instantly share code, notes, and snippets.

@HamGuy
Created August 28, 2013 03:35
Show Gist options
  • Save HamGuy/6361893 to your computer and use it in GitHub Desktop.
Save HamGuy/6361893 to your computer and use it in GitHub Desktop.
Win32下多线程同步技术
#include <iostream>
#include <windows.h>
#include <process.h>
int g_Count;
const int THREAD_NUM =10;
CRITICAL_SECTION g_csSubThread;
HANDLE g_Event;
unsigned int __stdcall ThreadFunc(PVOID pM)
{
int threadNum =*(int *)pM;
SetEvent(g_Event); //触发事件
Sleep(100); //模拟耗时操作
EnterCriticalSection(&g_csSubThread); //进去子线程临界区
g_Count++;
printf("Sub Thread %d Said : %d \n",threadNum,g_Count);
LeaveCriticalSection(&g_csSubThread); //操作完毕,离开子线程临界区
return 0;
}
int main()
{
printf("多线程自动事件同步实例\n");
InitializeCriticalSection(&g_csSubThread);
//创捷自动同步事件
g_Event=CreateEvent(NULL,FALSE,FALSE,NULL); //第二个参数为FALSE表示自动置位
g_Count=0;
HANDLE handles[THREAD_NUM];
int i =0;
while (i<THREAD_NUM)
{
handles[i]=(HANDLE)_beginthreadex(NULL,0,ThreadFunc,&i,0,NULL);
WaitForSingleObject(g_Event,INFINITE); // 等待事件被触发 ,由于是自动事件,该函数调用完毕会自动调用ResetEvent
i++;
}
WaitForMultipleObjects(THREAD_NUM,handles,TRUE,INFINITE);
CloseHandle(g_Event); //销毁事件
DeleteCriticalSection(&g_csSubThread);//销毁临界区
return 0;
}
#include <iostream>
#include <windows.h>
#include <process.h>
int g_Count;
const int THREAD_NUM =10;
CRITICAL_SECTION g_csMainThread,g_csSubThread;
unsigned int __stdcall ThreadFunc(PVOID pM)
{
int threadNum =*(int *)pM;
//确保子线程创建成功,离开临界区
LeaveCriticalSection(&g_csMainThread);
Sleep(100); //模拟耗时操作
EnterCriticalSection(&g_csSubThread); //进去子线程临界区
g_Count++;
printf("Sub Thread %d Said : %d \n",threadNum,g_Count);
LeaveCriticalSection(&g_csSubThread); //操作完毕,离开子线程临界区
return 0;
}
int main()
{
printf("多线程临界区互斥实例\n");
//临界区初始化
InitializeCriticalSection(&g_csMainThread);
InitializeCriticalSection(&g_csSubThread);
g_Count=0;
HANDLE handles[THREAD_NUM];
int i =0;
while (i<THREAD_NUM)
{
EnterCriticalSection(&g_csMainThread); //主线程进去临界区,确保每一次只有一个线程被创建
handles[i]=(HANDLE)_beginthreadex(NULL,0,ThreadFunc,&i,0,NULL);
i++;
}
WaitForMultipleObjects(THREAD_NUM,handles,TRUE,INFINITE);
//销毁临界区
DeleteCriticalSection(&g_csSubThread);
DeleteCriticalSection(&g_csMainThread);
return 0;
}
#include <iostream>
#include <windows.h>
#include <process.h>
int g_Count;
const int THREAD_NUM =10;
CRITICAL_SECTION g_csSubThread;
HANDLE g_Event;
unsigned int __stdcall ThreadFunc2(PVOID pM)
{
int threadNum =*(int *)pM;
SetEvent(g_Event); //触发事件
Sleep(100); //模拟耗时操作
EnterCriticalSection(&g_csSubThread); //进去子线程临界区
g_Count++;
printf("Sub Thread %d Said : %d \n",threadNum,g_Count);
LeaveCriticalSection(&g_csSubThread); //操作完毕,离开子线程临界区
return 0;
}
int main()
{
printf("多线程手动事件同步实例\n");
//临界区初始化
InitializeCriticalSection(&g_csSubThread);
//创捷手动同步事件
g_Event=CreateEvent(NULL,TRUE,FALSE,NULL); //第二个参数为TRUE表示手动置位
g_Count=0;
HANDLE handles[THREAD_NUM];
int i =0;
while (i<THREAD_NUM)
{
handles[i]=(HANDLE)_beginthreadex(NULL,0,ThreadFunc2,&i,0,NULL);
WaitForSingleObject(g_Event,INFINITE); // 等待事件被触发
ResetEvent(g_Event); //手动置位,让事件处于未触发状态
i++;
}
WaitForMultipleObjects(THREAD_NUM,handles,TRUE,INFINITE);
CloseHandle(g_Event); //销毁事件
DeleteCriticalSection(&g_csSubThread);//销毁临界区
return 0;
}
/*
ref http://blog.csdn.net/morewindows/article/details/7470936
*/
#include <iostream>
#include <windows.h>
#include <process.h>
int g_Count;
const int THREAD_NUM =10;
CRITICAL_SECTION g_csSubThread;
HANDLE g_Mutex;
unsigned int __stdcall ThreadFunc(PVOID pM)
{
int threadNum =*(int *)pM;
ReleaseMutex(g_Mutex);//触发互斥量
Sleep(100); //模拟耗时操作
EnterCriticalSection(&g_csSubThread);
g_Count++;
printf("Sub Thread %d Said : %d \n",threadNum,g_Count);
LeaveCriticalSection(&g_csSubThread);
return 0;
}
int main()
{
printf("多线程互斥量实例\n");
g_Mutex=CreateMutex(NULL,FALSE,NULL);
InitializeCriticalSection(&g_csSubThread);
g_Count=0;
HANDLE handles[THREAD_NUM];
int i =0;
while (i<THREAD_NUM)
{
handles[i]=(HANDLE)_beginthreadex(NULL,0,ThreadFunc,&i,0,NULL);
WaitForSingleObject(g_Mutex,INFINITE);
i++;
}
WaitForMultipleObjects(THREAD_NUM,handles,TRUE,INFINITE);
CloseHandle(g_Mutex);
DeleteCriticalSection(&g_csSubThread);
for (int i=0;i<THREAD_NUM;i++)
{
CloseHandle(handles[i]);
}
return 0;
}
#include <iostream>
#include <windows.h>
#include <process.h>
int g_Count;
const int THREAD_NUM =10;
CRITICAL_SECTION g_csSubThread;
HANDLE g_Semaphore;
unsigned int __stdcall ThreadFunc(PVOID pM)
{
int threadNum =*(int *)pM;
ReleaseSemaphore(g_Semaphore,1,NULL);//信号量+1
Sleep(100); //模拟耗时操作
EnterCriticalSection(&g_csSubThread);
g_Count++;
printf("Sub Thread %d Said : %d \n",threadNum,g_Count);
LeaveCriticalSection(&g_csSubThread);
return 0;
}
int main()
{
printf("多线程信号量同步实例\n");
g_Semaphore=CreateSemaphore(NULL, 0, 1, NULL);//当前0个资源,最大允许1个同时访
InitializeCriticalSection(&g_csSubThread);
g_Count=0;
HANDLE handles[THREAD_NUM];
int i =0;
while (i<THREAD_NUM)
{
handles[i]=(HANDLE)_beginthreadex(NULL,0,ThreadFunc,&i,0,NULL);
WaitForSingleObject(g_Semaphore,INFINITE);//等待信号量>0
i++;
}
WaitForMultipleObjects(THREAD_NUM,handles,TRUE,INFINITE);
CloseHandle(g_Semaphore);
DeleteCriticalSection(&g_csSubThread);
for (int i=0;i<THREAD_NUM;i++)
{
CloseHandle(handles[i]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment