关于mutex的使用问题

DontKissBossAss 2012-12-07 09:19:12
由于mutex具有查询式风格,因此在某些情况下,用起来比较简洁。 最近在工程中使用了这种互斥方法。可是由此引起的问题,确是接连不断。不得已,最终删除了对它的使用。

问题代码如下
int MutextTest( LPVOID lpParam )
{
DWORD dwCount=0;
DWORD dwWaitResult = WaitForSingleObject(ghMutex, INFINITE); //这有问题
if ( WAIT_OBJECT_0 != dwWaitResult )
{
return -1;
}

__try
{
// TODO: Do somethings here
}

__finally
{
if (! ReleaseMutex(ghMutex))
{
// Handle error.
}
}

return 0;
}


问题: WaitForSingleObject 函数永远是返回WAIT_TIMEOUT, 即使就在该函数内使用了mutex。

我想知道,难道是我用错了,还是说,我很牛逼的发现了ms的BUG?
...全文
754 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
DontKissBossAss 2012-12-07
  • 打赏
  • 举报
回复
引用 1 楼 zhanshen2891 的回复:
C/C++ code?12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929……
CSDN的引用有问题啊。 帅哥,你用过么?别找MSDN摘一点就算回事,我这个例子也是摘的。 超级简单的一个代码。我不大认为我会搞错。
zhanshen2891 2012-12-07
  • 打赏
  • 举报
回复
#include <windows.h>
#include <stdio.h>

#define THREADCOUNT 2

HANDLE ghMutex; 

DWORD WINAPI WriteToDatabase( LPVOID );

void main()
{
    HANDLE aThread[THREADCOUNT];
    DWORD ThreadID;
    int i;

    // Create a mutex with no initial owner

    ghMutex = CreateMutex( 
        NULL,              // default security attributes
        FALSE,             // initially not owned
        NULL);             // unnamed mutex

    if (ghMutex == NULL) 
    {
        printf("CreateMutex error: %d\n", GetLastError());
        return;
    }

    // Create worker threads

    for( i=0; i < THREADCOUNT; i++ )
    {
        aThread[i] = CreateThread( 
                     NULL,       // default security attributes
                     0,          // default stack size
                     (LPTHREAD_START_ROUTINE) WriteToDatabase, 
                     NULL,       // no thread function arguments
                     0,          // default creation flags
                     &ThreadID); // receive thread identifier

        if( aThread[i] == NULL )
        {
            printf("CreateThread error: %d\n", GetLastError());
            return;
        }
    }

    // Wait for all threads to terminate

    WaitForMultipleObjects(THREADCOUNT, aThread, TRUE, INFINITE);

    // Close thread and mutex handles

    for( i=0; i < THREADCOUNT; i++ )
        CloseHandle(aThread[i]);

    CloseHandle(ghMutex);
}

DWORD WINAPI WriteToDatabase( LPVOID lpParam )
{ 
    DWORD dwCount=0, dwWaitResult; 

    // Request ownership of mutex.

    while( dwCount < 20 )
    { 
        dwWaitResult = WaitForSingleObject( 
            ghMutex,    // handle to mutex
            INFINITE);  // no time-out interval
 
        switch (dwWaitResult) 
        {
            // The thread got ownership of the mutex
            case WAIT_OBJECT_0: 
                __try { 
                    // TODO: Write to the database
                    printf("Thread %d writing to database...\n", 
                           GetCurrentThreadId());
                    dwCount++;
                } 

                __finally { 
                    // Release ownership of the mutex object
                    if (! ReleaseMutex(ghMutex)) 
                    { 
                        // Deal with error.
                    } 
                } 
                break; 

            // The thread got ownership of an abandoned mutex
            case WAIT_ABANDONED: 
                return FALSE; 
        }
    }
    return TRUE; 
}
摘自MSDN,肯定是你哪里用的不对,看一下吧。你是不是提前把互斥锁句柄关闭了? MSDN上说这种情况下函数行为是未定义的。
swordtan 2012-12-07
  • 打赏
  • 举报
回复
来个全的代码吧,这部分代码看起来没问题
赵4老师 2012-12-07
  • 打赏
  • 举报
回复
《Windows核心编程》
DontKissBossAss 2012-12-07
  • 打赏
  • 举报
回复
人咋这么少呢?

24,860

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧