c++CreateEvent函数在多线程中使用及实例

http://blog.csdn.net/richerg85/article/details/7471426

 

 

c++CreateEvent函数在多线程中使用及实例

CreateEvent函数详解参见本博客文章:

c++中CreateEvent函数解析(2)

HANDLE CreateEvent(
LPSECURITY_ATTRIBUTES lpEventAttributes,
BOOL bManualReset, 
BOOL bInitialState,
LPCSTR lpName
);
bManualReset:TRUE,使用ResetEvent()手动重置为无信号状态;FALSE,当一个等待线程被释放时,自动重置状态为无信号状态。

bInitialState:指定事件对象的初始状态,当TRUE,初始状态为有信号状态;当FALSE,初始状态为无信号状态。

下面主要演示一下采用CreateEvent实现多线程。

例子很简单,主要测试CreateEvent中bManualReset:和bInitialState参数的取值在线程调用中信号状态的情况。


测试1:

bManualReset:TRUE
bInitialState:TRUE

CreateEvent(NULL, TRUE, TRUE, NULL); //使用手动重置为无信号状态,初始化时有信号状态

example.cpp

#include "iostream"
#include "windows.h"
using namespace std;

DWORD WINAPI ThreadProc1(LPVOID lpParam);
DWORD WINAPI ThreadProc2(LPVOID lpParam);
HANDLE hEvent = NULL;
HANDLE hThread1 = NULL;
HANDLE hThread2 = NULL;
int main(int argc, char *args[])
{
	hEvent = CreateEvent(NULL, TRUE, TRUE, NULL); //使用手动重置为无信号状态,初始化时有信号状态
	//hEvent = CreateEvent(NULL, FALSE, TRUE, NULL); //当一个等待线程被释放时,自动重置为无信号状态,初始是有信号状态
	//if (SetEvent(hEvent))
	//{
	//	cout << "setEvent 成功" <<endl;
	//}
	hThread1 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadProc1, NULL, 0,NULL);
	Sleep(200);
	hThread2 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadProc2, NULL, 0,NULL);
	Sleep(200);
	if ( NULL == hThread1)
	{
		cout <<"create thread fail!";
	}
	//DWORD dCount = ResumeThread(hThread);
	//cout << LOWORD(dCount) << endl;
	return 0;
}
DWORD WINAPI ThreadProc1(LPVOID lpParam)
{
	cout <<"in thread1@!"<<endl;
	
	DWORD dReturn = WaitForSingleObject(hEvent,INFINITE);
	
	if ( WAIT_OBJECT_0 == dReturn)
	{
		cout <<" thread1 signaled ! "<<endl;
	}
	cout <<"in thread1 --signal"<<endl;
	
	//SetEvent(hEvent);
	return 0;
}
DWORD WINAPI ThreadProc2(LPVOID lpParam)
{
	cout <<"in thread2@!"<<endl;
	
	DWORD dReturn = WaitForSingleObject(hEvent,INFINITE);
	
	if ( WAIT_OBJECT_0 == dReturn)
	{
		cout <<"thread2 signaled ! "<<endl;
	}
	cout <<"in thread2--signal"<<endl;
	
	return 0;
}

执行结果:

c++CreateEvent函数在多线程中使用及实例_第1张图片

从结果中看,执行完线程1又执行了线程2.

由于hEvent = CreateEvent(NULL, TRUE, TRUE, NULL),使用手动重置为无信号状态,初始化时有信号状态

所以hEvent一直处于有信号状态,无论是线程1释放后,hEvent仍处于有信号状态,所以线程2正常执行了。


测试2:

bManualReset:FALSE
bInitialState:TRUE

hEvent = CreateEvent(NULL, FALSE, TRUE, NULL); //当一个等待线程被释放时,自动重置为无信号状态,初始是有信号状态

example2.cpp

#include "iostream"
#include "windows.h"
using namespace std;

DWORD WINAPI ThreadProc1(LPVOID lpParam);
DWORD WINAPI ThreadProc2(LPVOID lpParam);
HANDLE hEvent = NULL;
HANDLE hThread1 = NULL;
HANDLE hThread2 = NULL;
int main(int argc, char *args[])
{
	//hEvent = CreateEvent(NULL, TRUE, TRUE, NULL); //使用手动重置为无信号状态,初始化时有信号状态
	hEvent = CreateEvent(NULL, FALSE, TRUE, NULL); //当一个等待线程被释放时,自动重置为无信号状态,初始是有信号状态
	//if (SetEvent(hEvent))
	//{
	//	cout << "setEvent 成功" <<endl;
	//}
	hThread1 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadProc1, NULL, 0,NULL);
	Sleep(200);
	hThread2 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadProc2, NULL, 0,NULL);
	Sleep(200);
	if ( NULL == hThread1)
	{
		cout <<"create thread fail!";
	}
	//DWORD dCount = ResumeThread(hThread);
	//cout << LOWORD(dCount) << endl;
	return 0;
}
DWORD WINAPI ThreadProc1(LPVOID lpParam)
{
	cout <<"in thread1@!"<<endl;
	
	DWORD dReturn = WaitForSingleObject(hEvent,INFINITE);
	
	if ( WAIT_OBJECT_0 == dReturn)
	{
		cout <<" thread1 signaled ! "<<endl;
	}
	cout <<"in thread1 --signal"<<endl;
	
	//SetEvent(hEvent);
	return 0;
}
DWORD WINAPI ThreadProc2(LPVOID lpParam)
{
	cout <<"in thread2@!"<<endl;
	
	DWORD dReturn = WaitForSingleObject(hEvent,INFINITE);
	
	if ( WAIT_OBJECT_0 == dReturn)
	{
		cout <<"thread2 signaled ! "<<endl;
	}
	cout <<"in thread2--signal"<<endl;
	
	return 0;
}
执行结果:


从执行结果中分析,执行了线程1,线程2一直在等待,直到主线程结束。

由于hEvent = CreateEvent(NULL, FALSE, TRUE, NULL),当一个等待线程被释放时,自动重置为无信号状态,初始是有信号状态

初始执行线程1的时候,hEvent是有信号的,所以线程1正常执行;又由于bManualReset=FALSE,所以执行完线程1后,hEvent自动重置为无信号状态,所以在线程2中,

WaitForSingleObject(hEvent,INFINITE);
函数一直在等待hEvent变为有信号状态,但是当主线程执行完,还没等待到,线程2程序一直没有走下去。


测试3:

bManualReset:TRUE
bInitialState:FALSE

hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);//使用手动重置为无信号状态,初始化时为无信号状态

example3.cpp

#include "iostream"
#include "windows.h"
using namespace std;

DWORD WINAPI ThreadProc1(LPVOID lpParam);
DWORD WINAPI ThreadProc2(LPVOID lpParam);
HANDLE hEvent = NULL;
HANDLE hThread1 = NULL;
HANDLE hThread2 = NULL;
int main(int argc, char *args[])
{
	//hEvent = CreateEvent(NULL, TRUE, TRUE, NULL); //使用手动重置为无信号状态,初始化时有信号状态
	//hEvent = CreateEvent(NULL, FALSE, TRUE, NULL); //当一个等待线程被释放时,自动重置为无信号状态,初始是有信号状态
	hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);//使用手动重置为无信号状态,初始化时为无信号状态
	//if (SetEvent(hEvent))
	//{
	//	cout << "setEvent 成功" <<endl;
	//}
	hThread1 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadProc1, NULL, 0,NULL);
	Sleep(200);
	hThread2 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadProc2, NULL, 0,NULL);
	Sleep(200);
	if ( NULL == hThread1)
	{
		cout <<"create thread fail!";
	}
	//DWORD dCount = ResumeThread(hThread);
	//cout << LOWORD(dCount) << endl;
	return 0;
}
DWORD WINAPI ThreadProc1(LPVOID lpParam)
{
	cout <<"in thread1@!"<<endl;
	
	DWORD dReturn = WaitForSingleObject(hEvent,INFINITE);
	
	if ( WAIT_OBJECT_0 == dReturn)
	{
		cout <<" thread1 signaled ! "<<endl;
	}
	cout <<"in thread1 --signal"<<endl;
	
	//SetEvent(hEvent);
	return 0;
}
DWORD WINAPI ThreadProc2(LPVOID lpParam)
{
	cout <<"in thread2@!"<<endl;
	
	DWORD dReturn = WaitForSingleObject(hEvent,INFINITE);
	
	if ( WAIT_OBJECT_0 == dReturn)
	{
		cout <<"thread2 signaled ! "<<endl;
	}
	cout <<"in thread2--signal"<<endl;
	
	return 0;
}

执行结果,可想而知,只能输出:

in thread1@!
in thread2@!
因为初始为无信号状态,所以hEvent一直处于无信号状态,因此这两个线程一直在等待,直到主线程结束。


修改:放开例子中的注释部分:

if (SetEvent(hEvent))//设置信号为有信号状态
{
cout << "setEvent 成功" <<endl;
}

执行结果:

c++CreateEvent函数在多线程中使用及实例_第2张图片

可见,线程1和线程2都执行了。

因为调用SetEvent,事件变为有信号状态,线程1执行;又由于线程1释放后,hEvent仍旧处于有信号状态,所以线程2也执行了。


再修改:在线程1中,添加ResetEvent(hEvent)(手动设置事件为无信号状态),则线程2不会执行。


测试4:

bManualReset:FALSE
bInitialState:FALSE

hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);//线程释放后自动重置为无信号状态,初始化时为无信号状态

example4.cpp

#include "iostream"
#include "windows.h"
using namespace std;

DWORD WINAPI ThreadProc1(LPVOID lpParam);
DWORD WINAPI ThreadProc2(LPVOID lpParam);
HANDLE hEvent = NULL;
HANDLE hThread1 = NULL;
HANDLE hThread2 = NULL;
int main(int argc, char *args[])
{
	//hEvent = CreateEvent(NULL, TRUE, TRUE, NULL); //使用手动重置为无信号状态,初始化时有信号状态
	//hEvent = CreateEvent(NULL, FALSE, TRUE, NULL); //当一个等待线程被释放时,自动重置为无信号状态,初始是有信号状态
	//hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);//使用手动重置为无信号状态,初始化时为无信号状态
	hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);//使用手动重置为无信号状态,初始化时为无信号状态
	if (SetEvent(hEvent))
	{
		cout << "setEvent 成功" <<endl;
	}
	hThread1 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadProc1, NULL, 0,NULL);
	Sleep(200);
	hThread2 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadProc2, NULL, 0,NULL);
	Sleep(200);
	if ( NULL == hThread1)
	{
		cout <<"create thread fail!";
	}
	//DWORD dCount = ResumeThread(hThread);
	//cout << LOWORD(dCount) << endl;
	return 0;
}
DWORD WINAPI ThreadProc1(LPVOID lpParam)
{
	cout <<"in thread1@!"<<endl;
	
	DWORD dReturn = WaitForSingleObject(hEvent,INFINITE);
	
	if ( WAIT_OBJECT_0 == dReturn)
	{
		cout <<" thread1 signaled ! "<<endl;
	}
	cout <<"in thread1 --signal"<<endl;
	
	//SetEvent(hEvent);
	return 0;
}
DWORD WINAPI ThreadProc2(LPVOID lpParam)
{
	cout <<"in thread2@!"<<endl;
	
	DWORD dReturn = WaitForSingleObject(hEvent,INFINITE);
	
	if ( WAIT_OBJECT_0 == dReturn)
	{
		cout <<"thread2 signaled ! "<<endl;
	}
	cout <<"in thread2--signal"<<endl;
	
	return 0;
}

执行结果:

c++CreateEvent函数在多线程中使用及实例_第3张图片

由于调用SetEvent,hEvent为有信号状态,线程1正常执行,又由于调用完线程1后,hEvent自动重置为无信号状态,所以线程2只能在等待,直到主线程退出。

修改:线程1中的SetEvent(hEvent);的注释去掉,再运行,则线程1和线程2 都会执行。

 

 

你可能感兴趣的:(c++CreateEvent函数在多线程中使用及实例)