我的程序中有两个线程(Thread1和Thread2),他们都会对同一数据进行修改,为了保证在同一时间内只有一个线程修改数据,我想用互斥量来同步两个线程。但总是出问题(runtime error).
我是这么做的:
1。在主程序中定义了互斥对象HANDLE m_hMutex = CreateMutex(NULL,FALSE,NULL);
其初始状态为未被激活的。
2。在Thread1的SetData(...)和Thread2的UpdateData(...)中我都是这样用互斥量的:
void SetData(...)
{
DWORD dw =::WaitForSingleObject(m_hMutex,INFINITE);
if(dw == WAIT_OBJECT_0)
{
 修改数据;
}
BOOL bm = ReleaseMutex(m_hMutex);
ASSERT(bm);
}void UpdateData(...)函数同上。但这样始终没办法解决两个线程的互斥问题。我是不是在使用上有误?
另外,互斥量初始状态为未被激活的,那么什么时候会被激活呢?

解决方案 »

  1.   

    你可以用CCriticalSection,我一般是用这个来做线程安全的。如果完全象你说的,因为你没有在其他处释放,应该是一直等待才对啊
      

  2.   

    ReleaseMutex(m_hMutex);
    你怎么释放了啊,应该在主程序里释放啊
      

  3.   

    CRITICAL_SECTION CriticalSection;
    InitializeCriticalSection(&CriticalSection);//初始化互斥区
    EnterCriticalSection(&CriticalSection);
    //读写数据
    LeaveCriticalSection(&CriticalSection);
      

  4.   

    你写的代码是没有问题的啊,应该是其它的代码的问题吧。
    当WaitForSingleObject函数成功返回时,互斥信号处于通知状态,调用ReleaseMutex函数时释放,互斥信号便处于未通知状态,其他线程就可以访问互斥信号
      

  5.   

    我也搞不清楚,我写的函数没有ReleaseMutex也可以工作,下次还是以为得到了信号
    谁能讲讲Mutex的使用方法呢
      

  6.   

    我想补充一下:
    Thread1就是我的主线程,Thread2运行于Thread1中,所以我不能让Thread1完全挂起,不然Thread2也不能运行了。所以我想在某一点(修改数据时)能够同步就可以了,不知我的想法对吗?
      

  7.   

    使用临界区是一种方法如果使用互斥量的话,加上互斥量的名称试试
    HANDLE m_hMutex = CreateMutex(NULL,FALSE, _T("The only one mutex name"));
    另外建议最好判断 m_hMutex 的返回值是否为 ERROR_INVALID_HANDLE
    你的思路应该是正确的
      

  8.   

    参考一下MSDN的例子
    HANDLE hMutex; // Create a mutex with no initial owner.hMutex = CreateMutex( 
        NULL,                       // no security attributes
        FALSE,                      // initially not owned
        "MutexToProtectDatabase");  // name of mutexif (hMutex == NULL) 
    {
        // Check for error.
    }
    When a thread of this process writes to the database, as in the next example, it first requests ownership of the mutex. If it gets ownership, the thread writes to the database and then releases its ownership.The example uses structured exception-handling syntax to ensure that the thread properly releases the mutex object. The __finally block of code is executed no matter how the __try block terminates (unless the __try block includes a call to the TerminateThread function). This prevents the mutex object from being abandoned inadvertently.BOOL FunctionToWriteToDatabase(HANDLE hMutex) 

        DWORD dwWaitResult;     // Request ownership of mutex.
     
        dwWaitResult = WaitForSingleObject( 
            hMutex,   // handle to mutex
            5000L);   // five-second time-out interval
     
        switch (dwWaitResult) 
        {
            // The thread got mutex ownership.
            case WAIT_OBJECT_0: 
                __try { 
                    // Write to the database.
                }             __finally { 
                    // Release ownership of the mutex object.
                    if (! ReleaseMutex(hMutex)) 
                    { 
                        // Deal with error.
                    }                 break; 
                }         // Cannot get mutex ownership due to time-out.
            case WAIT_TIMEOUT: 
                return FALSE;         // Got ownership of the abandoned mutex object.
            case WAIT_ABANDONED: 
                return FALSE; 
        }    return TRUE; 
    }
      

  9.   

    你的thread2是在thread1中创建的吗?要是那样的话,创建thread2的代码应该在thread1的下面这段代码之外:
    DWORD dw =::WaitForSingleObject(m_hMutex,INFINITE);
    if(dw == WAIT_OBJECT_0)
    {
     修改数据;
    }
    BOOL bm = ReleaseMutex(m_hMutex);
    这样应该是没有问题的
      

  10.   

    没错,我的thread2是在上面那段代码外创建的。
    我认为我的思路可能有问题,因为Thread1是主线程,其他任何线程都是在主线程中创建的,但现在Thread1要与Thread2互斥,那么当Thread2在获得CPU时间后,Thread1被阻塞会不会影响Thread2的运行?
    请大家帮我分析分析!