本帖最后由 GoForSky 于 2010-11-29 15:42:44 编辑

解决方案 »

  1.   

    此处的CloseHandle(hMutex)和ReleaseMute(hMutex)有什么区别?
      

  2.   

    LZ改成把线程暂停时间加长。太短的话,可能主线程还没释放句柄,线程就拥有互斥对像了。
    改为等待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;
        
    }
      

  3.   

    #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销毁吗???    Sleep(1000);
        cout<<"After sleep"<<endl;}DWORD WINAPI Fun1Proc(
      LPVOID lpParameter   // thread data
    )
    {    WaitForSingleObject(hMutex,INFINITE); //这里会报错,无效句柄
        cout<<"thread1 is running"<<endl;
        return 0;
        
    }
      

  4.   

    因为线程创建前,Mutex还没有创建
    如果线程和Mutex的创建顺序调换,如果马上CloseHandle(hMutex);
    线程也是WaitForSingleObject(hMutex,INFINITE); //这里会报错,无效句柄
      

  5.   


    #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;
        
    }
      

  6.   


    CloseHandle(hMutex),释放句柄资源
    ReleaseMutex(hMutex),释放互斥体拥有权
    对于这里,因为线程创和Mutex的创建顺序不确定(一般是先创建Mutex,再开线程)
    使用ReleaseMutex,WaitForSingleObject(hMutex,INFINITE);也是不确定
      

  7.   

    个人理解
    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是释放拥有权
      

  8.   

    CloseHandle(handle); 
    是关闭一个句柄,并将这个句柄的引用计数减一,如果这个句柄的引用计数减到0,那么操作系统将释放这个核心对象的句柄 ReleaseMutex();让当前线程释放对该互斥体的拥有权,把他交给另一个等待中的线程 
    WaitforsingleObject....... //等待获得对a的写的权利   a++   //保护部分
    ReleaseMutex......   //不需要保护了 允许其它线程写a/////////////////////////////////////////////////////////
    CloseHandle(hMutex)后,互斥对象的计数应该为0了,可是为什么线程1还运行呢???线程和线程句柄(Handle)不是一个东西,线程是在cpu上运行的.....(说不清楚了),线程句柄是一个内核对象。我们可以通过句柄来操作线程,但是线程的生命周期和线程句柄的生命周期不一样的。线程的生命周期就是线程函数从开始执行到return,线程句柄的生命周期是从CreateThread返回到你CloseHandle()。希望对你有用
      

  9.   


        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);这个函数是无效的.可以忽略
      

  10.   

    #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);    //销毁互斥对象,的确是销毁了    
    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);    
      

  11.   

    5楼和11楼正解
    问题出在WaitForSingleObject函数的返回值上。
    具体的解释去看msdn,很详细
      

  12.   

    内核计数器和句柄一点关系都没有,或者说最多是一个指向关系。CloseHandle是通过句柄将对应的计数器-1,这样handle就无效了,但是你创建的线程还存在,只是你的线程已经没有了这个线程的句柄