Win32 线程的创建&信号量临界资源&事件对象

时间:2014-04-29 13:31:21   收藏:0   阅读:377

背景:

写了个程序,主要实现的功能为主线程创建两个子线程,之后主线程进入等待事件的循环。子线程1对临界资源cnt的值减2并输出cnt的值。子线程2对cnt的值加1并输出cnt的值,且当cnt为50的时候触发事件对象hend_even,子进程2退出。主线程收到hend_even后杀死子线程1,之后进入scanf阻塞。。。

代码示例:

#include <windows.h>
#include <stdio.h>

DWORD WINAPI funAproc(LPVOID lpParameter);
DWORD WINAPI funBproc(LPVOID lpParameter);

int cnt=100;			//共享数据
HANDLE hmutex_cnt;		//cnt的信号量锁
HANDLE hend_even;		//事件

void main()
{
	HANDLE th1,th2;
	//false表示当前主线程不拥有该信号,true表示创建该信号后备主线程拥有
	hmutex_cnt = CreateMutex(NULL,FALSE,NULL);
	//第二个参数:
	//true(人工重置事件对象,等待该事件的所有进程都将进入运行
	//false(自动重置事件对象),等待该事件中的一个进程可变为调度
	//第三个参数:
	//true: 创建该事件对象后,触发事件,false:不触发事件。
	hend_even = CreateEvent(NULL,TRUE,FALSE,NULL);

	int x=2;//带参线程
	//funAproc为线程入口,&x为线程函数参数,该参数为(void*)类型,如果多余一个参数可以采用传递结构体的方式
	th1 = CreateThread(NULL,0,funAproc,&x,0,NULL);
	
	th2 = CreateThread(NULL,0,funBproc,NULL,0,NULL);

	while (TRUE)
	{
		if (cnt == 0) break;
		//Sleep(1);
		if(WAIT_OBJECT_0 == WaitForSingleObject(hend_even,100)){//主线程等待th2发出“终止th1的事件”,即hend_even
			TerminateThread(th1,1); //终止线程
			break;
		}
	}
	printf("back to main thread... \n ");
	int i;
	while (scanf("%d",&i))
	{
		;
	}
}

DWORD WINAPI funAproc(LPVOID lpParameter){
	int b,c;
	int k = *(int*)lpParameter;

	while (cnt>0)
	{
		WaitForSingleObject(hmutex_cnt,INFINITE);//获取信号量,对临界资源(cnt)进行W操作
		cnt -= k;
		printf("thread A delete 2 of cnt = %d\n",cnt);
		ReleaseMutex(hmutex_cnt);				//释放信号量
	}
	return 0;
}
DWORD WINAPI funBproc(LPVOID lpParameter){
	while (cnt>0)
	{
		WaitForSingleObject(hmutex_cnt,INFINITE);//获取信号量,对临界资源(cnt)进行W操作
		cnt = cnt + 1;
		printf("thread B add 1 of cnt = %d\n",cnt);

		if (cnt == 50)	//当cnt == 50的时候,终止th1和th2,此时th2触发hend_even事件,并退出
		{
			SetEvent(hend_even);
			printf("proc B stop to sail ticket...\n");
			ReleaseMutex(hmutex_cnt);	//释放信号量
			exit(0);
		}
		ReleaseMutex(hmutex_cnt);		//释放信号量
	}
	return 0;
}

运行结果:

thread A delete 2 of cnt = 98
thread B add 1 of cnt = 99
thread A delete 2 of cnt = 97
thread B add 1 of cnt = 98
thread A delete 2 of cnt = 96
thread B add 1 of cnt = 97
thread A delete 2 of cnt = 95
thread B add 1 of cnt = 96
thread A delete 2 of cnt = 94
thread B add 1 of cnt = 95
thread A delete 2 of cnt = 93
thread B add 1 of cnt = 94
thread A delete 2 of cnt = 92
thread B add 1 of cnt = 93
thread A delete 2 of cnt = 91
thread B add 1 of cnt = 92
thread A delete 2 of cnt = 90
thread B add 1 of cnt = 91
thread A delete 2 of cnt = 89
thread B add 1 of cnt = 90
thread A delete 2 of cnt = 88
thread B add 1 of cnt = 89
...
thread A delete 2 of cnt = 51
thread B add 1 of cnt = 52
thread A delete 2 of cnt = 50
thread B add 1 of cnt = 51
thread A delete 2 of cnt = 49
thread B add 1 of cnt = 50
proc B stop to sail ticket...
proc B stop to sail ticket...
back to main thread...


参考:

http://www.cnblogs.com/1314NNNKKK/p/3386375.html

http://hi.baidu.com/kdlipm/item/005be8eb29d785e1fa42bae5



评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!