#include <iostream>
#include <windows.h>
using namespace std;
int i=100;
HANDLE h0=NULL;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
DWORD WINAPI ThreadProc1(LPVOID pParam)
{
WaitForSingleObject(h0,INFINITE);
while(true)
{
if(i>0)
{
cout<<"1: "<<i--<<endl;
Sleep(1000);
}
else
{
cout<<"__"<<endl;
break;
}
ReleaseMutex(h0);
}
return 0;
}
DWORD WINAPI ThreadProc2(LPVOID pParam)
{
WaitForSingleObject(h0,INFINITE);
while(true)
{
if(i>0)
{
cout<<"2: "<<i--<<endl;
Sleep(1000);
}
else
{
cout<<"++"<<endl;
break;
}
ReleaseMutex(h0);
}
return 0;
}int main()
{
h0=CreateMutex(0,false,"abc");
HANDLE h[2];
h[0]=CreateThread(NULL,NULL,ThreadProc1,NULL,0,NULL);
h[1]=CreateThread(NULL,NULL,ThreadProc2,NULL,0,NULL);
WaitForMultipleObjects(2,h,true,INFINITE);
CloseHandle(h[0]);
CloseHandle(h[1]);
CloseHandle(h0);
return 0;
}
//前几行输出较混乱,帮忙解决一下
#include <windows.h>
using namespace std;
int i=100;
HANDLE h0=NULL;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
DWORD WINAPI ThreadProc1(LPVOID pParam)
{
WaitForSingleObject(h0,INFINITE);
while(true)
{
if(i>0)
{
cout<<"1: "<<i--<<endl;
Sleep(1000);
}
else
{
cout<<"__"<<endl;
break;
}
ReleaseMutex(h0);
}
return 0;
}
DWORD WINAPI ThreadProc2(LPVOID pParam)
{
WaitForSingleObject(h0,INFINITE);
while(true)
{
if(i>0)
{
cout<<"2: "<<i--<<endl;
Sleep(1000);
}
else
{
cout<<"++"<<endl;
break;
}
ReleaseMutex(h0);
}
return 0;
}int main()
{
h0=CreateMutex(0,false,"abc");
HANDLE h[2];
h[0]=CreateThread(NULL,NULL,ThreadProc1,NULL,0,NULL);
h[1]=CreateThread(NULL,NULL,ThreadProc2,NULL,0,NULL);
WaitForMultipleObjects(2,h,true,INFINITE);
CloseHandle(h[0]);
CloseHandle(h[1]);
CloseHandle(h0);
return 0;
}
//前几行输出较混乱,帮忙解决一下
HANDLE hThread2;
hThread1=CreateThread(NULL,0,Thread1Proc,NULL,0,NULL);
hThread2=CreateThread(NULL,0,Thread2Proc,NULL,0,NULL);
CloseHandle(hThread1);//关闭句柄, 此时系统会递减新线程的线程内核对象的使用计数, 当创建的线程
CloseHandle(hThread2);//执行完毕后, 系统也会递减线程内核对象的使用计数, 计数为0时, 系统释放线程内核对象
//不是关闭了,为什么还会执行Thread1Proc,Thread2Proc
hMutex=CreateMutex(NULL,TRUE,NULL); //TRUE表示调用CreateMutex的线程拥有了互斥对象
WaitForSingleObject(hMutex,INFINITE);//线程多次拥有互斥对象, 主要通过互斥对象内的计数器实现
ReleaseMutex(hMutex); //申请两次则要释放两次
ReleaseMutex(hMutex); //谁申请谁释放
Sleep(1000); //要保证在卖完100张票前,主线程不能退出