删除其中的一句ReleaseMutex,但发现这样做会导致互斥体不能被正常释放。请问这是怎么回事?

先上代码吧:
//这是2个线程模拟卖火车票的小程序
#include <windows.h>
#include <iostream>
#include <tchar.h>
using namespace std;

DWORD WINAPI Fun1Proc(LPVOID lpParameter);//thread data
DWORD WINAPI Fun2Proc(LPVOID lpParameter);//thread data

int index=0;
int tickets=10;
HANDLE hMutex;
void main()
{
HANDLE hThread1;
HANDLE hThread2;
//创建线程

hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
hThread2=CreateThread(NULL,0,Fun2Proc,NULL,0,NULL);
CloseHandle(hThread1);
CloseHandle(hThread2);

//创建互斥对象
hMutex=CreateMutex(NULL,TRUE,_T("tickets"));//创建互斥体 一次运行一个线程
if (hMutex)
{
if (ERROR_ALREADY_EXISTS==GetLastError())
{
cout<<"only one instance can run!"<<endl;
return;
}
}

WaitForSingleObject(hMutex,INFINITE);//等待进入互斥体 INFINITE -1
ReleaseMutex(hMutex);
ReleaseMutex(hMutex);

Sleep(3000);
}
//线程1的入口函数
DWORD WINAPI Fun1Proc(LPVOID lpParameter)//thread data
{
while (true)
{
ReleaseMutex(hMutex);
WaitForSingleObject(hMutex,INFINITE);
if (tickets>0)
{
Sleep(500);
cout<<"thread1 sell ticket :"<<tickets--<<endl;
}
else
break;
ReleaseMutex(hMutex);
}
return 0;
}
//线程2的入口函数
DWORD WINAPI Fun2Proc(LPVOID lpParameter)//thread data
{
while (true)
{
ReleaseMutex(hMutex);
WaitForSingleObject(hMutex,INFINITE);
if (tickets>0)
{
Sleep(500);
cout<<"thread2 sell ticket :"<<tickets--<<endl;
}
else
break;
ReleaseMutex(hMutex);
}
return 0;
}

在上面的代码是一个多线程的模型,但这个模型中有一个很有趣的地方:
原始作者用WaitForSingleObject(hMutex,INFINITE);语句来获得互斥体,同时用ReleaseMutex(hMutex);语句来释放互斥体,但仔细看代码却可以发现,每一个WaitForSingleObject其实是对应了两句一摸一样的释放语句,看上去好像是每获得一次互斥体就要释放两遍一样。

慕尼黑5688855
浏览 92回答 1
1回答

holdtom

一个线程可以重复占有mutex, 占有几次资源, 就要释放几次资源, 不然别的线程没法获得mutexmain()线程在CreateMutex(TRUE)的时候已经占用资源, 在WaitForSingleObject( hMutex )再次占用, 他需要释放两次同样的道理. 别问我你程序的逻辑,&nbsp;我不懂 &nbsp; 刚启动时,&nbsp;hMutex还没被创建,&nbsp;所以hMutex&nbsp;=&nbsp;0&nbsp;; DWORD&nbsp;WINAPI&nbsp;Fun1Proc(LPVOID&nbsp;lpParameter)//thread&nbsp;data { while&nbsp;(true) { ReleaseMutex(hMutex);&nbsp;//&nbsp;hMutex&nbsp;=&nbsp;0&nbsp;,未定义行为 WaitForSingleObject(hMutex,INFINITE);&nbsp;//&nbsp;hMutex&nbsp;=&nbsp;0&nbsp;,&nbsp;未定义 &nbsp; 这程序依赖于API的失败语义成为其逻辑一部分,&nbsp;所以阅读的人会很困难 ReleaseMutex(&nbsp;0&nbsp;)失败返回 WaitForSingleObject(&nbsp;0&nbsp;)失败返回 所以ReleaqseMutex只有被执行一次,&nbsp;没有两次.
打开App,查看更多内容
随时随地看视频慕课网APP