个人理解 1.CloseHandle(hMutex); //能将hMutex销毁吗??? 能,引用计数变为0时,系统自动删除内核对象。 2.CloseHandle(hMutex)后,互斥对象的计数应该为0了,可是为什么线程1还运行呢??? 在MSDN对WaitForSingleObject有这样的解释:If this handle is closed while the wait is still pending, the function's behavior is undefined.相当于WaitForSingleObject(NULL,INFINITE);这个函数是无效的.可以忽略 3.此处的CloseHandle(hMutex)和ReleaseMute(hMutex)有什么区别? Use the CloseHandle function to close the handle,ReleaseMute是释放拥有权
}结果还是显示: cout<<"thread1 is running"<<endl; 答案应该是如:hgy413 在MSDN对WaitForSingleObject有这样的解释: If this handle is closed while the wait is still pending(在进行中的;即将发生的,), the function's behavior is undefined.相当于WaitForSingleObject(NULL,INFINITE);这个函数是无效的.可以忽略
DWORD dwRet = WaitForSingleObject(hMutex,INFINITE); //If this handle is closed while the wait is still pending, //the function's behavior is [color=#FF0000]undefined[/color]. //当hMutex句柄关闭时,函数行为未决,所以可以返回,执行后面代码 //If the function fails, the return value is WAIT_FAILED if (dwRet==WAIT_FAILED) { cout<<"waittting failing!"<<endl; } switch(dwRet) { case WAIT_ABANDONED: cout<<"return by abandoning"<<endl; break; case WAIT_OBJECT_0: cout<<"Ok"<<endl; break; case WAIT_TIMEOUT: cout<<"return by timeout"<<endl; default: break; } cout<<"thread1 is running"<<endl; return 0;
改为等待4秒后就有效果了。
CloseHandle(hMutex)、
释放句柄
ReleaseMute(hMutex)
释放拥有权#include <windows.h>
#include <iostream.h>DWORD WINAPI Fun1Proc(
LPVOID lpParameter // thread data
);
HANDLE hMutex;
void main()
{
HANDLE hThread1;
hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
CloseHandle(hThread1); hMutex=CreateMutex(NULL,FALSE,"tickets");
CloseHandle(hMutex); //能将hMutex销毁吗??? while(1)
{
Sleep(1000);
cout<<"After sleep"<<endl;
}}DWORD WINAPI Fun1Proc(
LPVOID lpParameter // thread data
)
{
Sleep(4000);
cout<<"After sleep2"<<endl; WaitForSingleObject(hMutex,INFINITE);
cout<<"thread1 is running"<<endl;
return 0;
}
#include <iostream.h>DWORD WINAPI Fun1Proc(
LPVOID lpParameter // thread data
);
HANDLE hMutex;
void main()
{
HANDLE hThread1;
hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
CloseHandle(hThread1); hMutex=CreateMutex(NULL,FALSE,"tickets");
CloseHandle(hMutex); //能将hMutex销毁吗??? Sleep(1000);
cout<<"After sleep"<<endl;}DWORD WINAPI Fun1Proc(
LPVOID lpParameter // thread data
)
{ WaitForSingleObject(hMutex,INFINITE); //这里会报错,无效句柄
cout<<"thread1 is running"<<endl;
return 0;
}
如果线程和Mutex的创建顺序调换,如果马上CloseHandle(hMutex);
线程也是WaitForSingleObject(hMutex,INFINITE); //这里会报错,无效句柄
#include <windows.h>
#include <iostream.h>DWORD WINAPI Fun1Proc(
LPVOID lpParameter // thread data
);
HANDLE hMutex;
void main()
{
hMutex=CreateMutex(NULL,FALSE,"tickets"); HANDLE hThread1;
hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
CloseHandle(hThread1); Sleep(1000); CloseHandle(hMutex); //Äܽ«hMutexÏú»ÙÂ𣿣¿£¿ cout<<"After sleep"<<endl;
}DWORD WINAPI Fun1Proc(
LPVOID lpParameter // thread data
)
{
DWORD dwRet = WaitForSingleObject(hMutex,INFINITE);
switch(dwRet)
{
case WAIT_ABANDONED:
cout<<"failed"<<endl;
break; case WAIT_OBJECT_0:
cout<<"ok"<<endl;
break; default:
break;
}
cout<<"thread1 is running"<<endl;
return 0;
}
CloseHandle(hMutex),释放句柄资源
ReleaseMutex(hMutex),释放互斥体拥有权
对于这里,因为线程创和Mutex的创建顺序不确定(一般是先创建Mutex,再开线程)
使用ReleaseMutex,WaitForSingleObject(hMutex,INFINITE);也是不确定
1.CloseHandle(hMutex); //能将hMutex销毁吗???
能,引用计数变为0时,系统自动删除内核对象。
2.CloseHandle(hMutex)后,互斥对象的计数应该为0了,可是为什么线程1还运行呢???
在MSDN对WaitForSingleObject有这样的解释:If this handle is closed while the wait is still pending, the function's behavior is undefined.相当于WaitForSingleObject(NULL,INFINITE);这个函数是无效的.可以忽略
3.此处的CloseHandle(hMutex)和ReleaseMute(hMutex)有什么区别?
Use the CloseHandle function to close the handle,ReleaseMute是释放拥有权
是关闭一个句柄,并将这个句柄的引用计数减一,如果这个句柄的引用计数减到0,那么操作系统将释放这个核心对象的句柄 ReleaseMutex();让当前线程释放对该互斥体的拥有权,把他交给另一个等待中的线程
WaitforsingleObject....... //等待获得对a的写的权利 a++ //保护部分
ReleaseMutex...... //不需要保护了 允许其它线程写a/////////////////////////////////////////////////////////
CloseHandle(hMutex)后,互斥对象的计数应该为0了,可是为什么线程1还运行呢???线程和线程句柄(Handle)不是一个东西,线程是在cpu上运行的.....(说不清楚了),线程句柄是一个内核对象。我们可以通过句柄来操作线程,但是线程的生命周期和线程句柄的生命周期不一样的。线程的生命周期就是线程函数从开始执行到return,线程句柄的生命周期是从CreateThread返回到你CloseHandle()。希望对你有用
Sleep(30000); //等待30秒
cout<<"After sleep"<<endl;}DWORD WINAPI Fun1Proc(
LPVOID lpParameter // thread data
)
{
Sleep(20); //等待20秒,给系统足够的时间,20秒后应该将互斥对象销毁了呀
WaitForSingleObject(hMutex,INFINITE);
cout<<"thread1 is running"<<endl;
return 0;
}结果还是显示:
cout<<"thread1 is running"<<endl;
答案应该是如:hgy413
在MSDN对WaitForSingleObject有这样的解释:
If this handle is closed while the wait is still pending(在进行中的;即将发生的,), the function's behavior is undefined.相当于WaitForSingleObject(NULL,INFINITE);这个函数是无效的.可以忽略
#include <iostream.h>DWORD WINAPI Fun1Proc(
LPVOID lpParameter // thread data
);
HANDLE hMutex;
void main()
{
HANDLE hThread1;
hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
CloseHandle(hThread1);
hMutex=CreateMutex(NULL,FALSE,"tickets");
CloseHandle(hMutex); //销毁互斥对象,的确是销毁了
Sleep(1000);
cout<<"After sleep"<<endl;
}DWORD WINAPI Fun1Proc(
LPVOID lpParameter // thread data
)
{
DWORD dwRet = WaitForSingleObject(hMutex,INFINITE);
//If this handle is closed while the wait is still pending,
//the function's behavior is [color=#FF0000]undefined[/color].
//当hMutex句柄关闭时,函数行为未决,所以可以返回,执行后面代码
//If the function fails, the return value is WAIT_FAILED
if (dwRet==WAIT_FAILED)
{
cout<<"waittting failing!"<<endl;
}
switch(dwRet)
{
case WAIT_ABANDONED:
cout<<"return by abandoning"<<endl;
break;
case WAIT_OBJECT_0:
cout<<"Ok"<<endl;
break;
case WAIT_TIMEOUT:
cout<<"return by timeout"<<endl;
default:
break;
} cout<<"thread1 is running"<<endl;
return 0;
}CloseHandle(hMutex);
问题出在WaitForSingleObject函数的返回值上。
具体的解释去看msdn,很详细