因此,我只是在学习c ++互斥锁,并且在大多数情况下,我将遵循MSDN上的示例。为什么我超时?我使用Sleep()将互斥锁超时设置为2000ms,将“伪造”进程设置为250ms。您可以看到它处理得很好,然后开始炸毁。...我知道如果我将互斥锁超时提高到大约60000ms,那会很好,但是为什么我只希望250ms的过程那么高呢?另外,为什么它从线程ID#1跳到线程ID#25?

谢谢!
埃里克

http://msdn.microsoft.com/en-us/library/windows/desktop/ms686927(v=vs.85).aspx

int createMutex(char* mutexName)
{

#define THREADCOUNT 25

    HANDLE aThread[THREADCOUNT];
    DWORD ThreadID;
    int i;

    int ID[THREADCOUNT];

    // Create a mutex with no initial owner

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

    if (ghMutex == NULL)
    {
        return 1;
    }

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

        if( aThread[i] == NULL )
        {
            return 1;
        }
    }

    // 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);

    return 0;

}

DWORD WINAPI WriteToDatabase(int *ID){

    int threadID = *ID;

    char buffer[256];
    int MUTEX_TIMEOUT = 2000;
    int FAKE_PROCESS_TIME_DELAY = 250;

    DWORD dwWaitResult;

    // Request ownership of mutex.
    dwWaitResult = WaitForSingleObject(
        ghMutex,    // handle to mutex
        MUTEX_TIMEOUT);     // time-out interval

    sprintf(buffer, "NEW THREAD STARTED: #%d\n", threadID);
    printf(buffer);

    if(dwWaitResult == WAIT_OBJECT_0){

        // The thread got ownership of the mutex
        sprintf(buffer, "DB WRITE STATED: #%d\n", threadID);
        printf(buffer);

        Sleep(FAKE_PROCESS_TIME_DELAY); //simulate a long running process (db process?) which creates a WAIT_TIMEOUT

        sprintf(buffer, "DB WRITE COMPLETED: #%d\n", threadID);
        printf(buffer);

        dwCount++;
        ReleaseMutex(ghMutex);

        return TRUE;
    }else{
        switch(dwWaitResult){
                case WAIT_ABANDONED:
                    sprintf(buffer, "MUTEX ERROR [%s] #%d\n", "WAIT_ABANDONED", threadID);
                    break;
                case WAIT_TIMEOUT:
                    sprintf(buffer, "MUTEX ERROR [%s] #%d\n", "WAIT_TIMEOUT", threadID);
                    break;
                default:
                    sprintf(buffer, "MUTEX ERROR [%s] #%d\n", "UNKNOWN", threadID);
        }

        printf(buffer);
        MutexERRORs++;

        //ReleaseMutex(ghMutex);
        return FALSE;
    }

    return TRUE;
}

最佳答案

您几乎同时创建了25个线程,如果每个线程使用互斥体约250ms,则每个线程背对背运行时,所有线程要处理的总时间将为250ms * 25 = 6250ms。考虑到这一点,您的某些线程肯定会超时获取互斥对象,因为您仅等待2000ms。的确,在处理了8个线程之后,剩下的超时看起来就很奇怪了(因为250ms * 8 = 2000ms,这不足为奇)。

至于线程25在线程1之后获得互斥体,我不认为在等待与获取互斥体方面有任何保证的顺序。多次运行它,每次运行可能会获得不同的顺序。

关于c++ - VC++ Mutex问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21647921/

10-11 16:01