您当前的位置: 首页 > 

txwtech

暂无认证

  • 4浏览

    0关注

    813博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

MFC信号量对象实现多线程范例

txwtech 发布时间:2019-03-26 11:23:19 ,浏览量:4

 

 

MFC信号量对象实现多线程范例

// vs23_5_7.cpp : 定义控制台应用程序的入口点。
//txwtech vs2010

#include "stdafx.h"
#include "windows.h"
DWORD WINAPI ThreadProc1(LPVOID lpParam);
DWORD WINAPI ThreadProc2(LPVOID lpParam);
int iGolbalCount=0;
int iMax=12;
int cMax=1;
HANDLE hSemaphore;
int _tmain(int argc, _TCHAR* argv[])
{
	
	HANDLE hThread1,hThread2;
//	hSemaphore=CreateSemaphore(NULL,cMax,iMax,NULL);//
	hSemaphore=CreateSemaphore(NULL,cMax,cMax,NULL);
	if(hSemaphore==NULL)
	{
		printf("创建信号量对象失败");
		return 0;
	}
	hThread1=CreateThread(NULL,0,ThreadProc1,NULL,0,NULL);
	hThread2=CreateThread(NULL,0,ThreadProc2,NULL,0,NULL);
	Sleep(50000);
	printf("主线程结束\n");
	Sleep(30000);
	return 0;
}
DWORD WINAPI ThreadProc1(LPVOID lpParam)
{
	long count;
	while(true)
	{
    DWORD dwWaitResult=WaitForSingleObject(hSemaphore,0L);//等待时间太短
	//DWORD dwWaitResult=WaitForSingleObject(hSemaphore,INFINITE);//无限等待
	if(dwWaitResult==WAIT_OBJECT_0)
	 {
		if(iGolbalCount            
关注
打赏
1665060526
查看更多评论
0.0508s