您当前的位置: 首页 > 

txwtech

暂无认证

  • 4浏览

    0关注

    813博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

MFC互斥对象实现多线程同步实例

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

MFC互斥对象实现多线程同步实例

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

#include "stdafx.h"
#include "windows.h"
DWORD WINAPI ThreadProc1(LPVOID lpParam);
DWORD WINAPI ThreadProc2(LPVOID lpParam);
int iGolbalCount=0;
int iMax=12;
HANDLE hMutex;



int _tmain(int argc, _TCHAR* argv[])
{
	HANDLE hThread1,hThread2;
	hThread1=CreateThread(NULL,0,ThreadProc1,NULL,0,NULL);
	hThread2=CreateThread(NULL,0,ThreadProc2,NULL,0,NULL);
	hMutex=CreateMutex(NULL,true,LPCTSTR("iGobalCount"));
	//hMutex=CreateMutex(NULL,false,LPCTSTR("iGobalCount"));
	if(hMutex==NULL)
	{
		printf("创建互斥对象失败\r\n");
		return 0;
	}
	WaitForSingleObject(hMutex,INFINITE);
	ReleaseMutex(hMutex);
	ReleaseMutex(hMutex);
	Sleep(10000);
	//CloseHandle(hThread1);
//	CloseHandle(hThread2);
	
	printf("主线程结束!");
	Sleep(5000);

	return 0;
}
DWORD WINAPI ThreadProc1(LPVOID lpParam)
{
	while(true)
	{
		WaitForSingleObject(hMutex,INFINITE);
		if(iGolbalCount            
关注
打赏
1665060526
查看更多评论
0.0531s