VC ++ Mutex问题

时间:2014-02-08 15:34:51

标签: c++ multithreading mutex

所以我只是在学习c ++互斥体,而且我在大多数情况下都在关注MSDN上的例子。我为什么要超时?我将互斥锁超时设置为2000ms,使用Sleep()将“假”进程设置为250ms。你可以看到它处理得很好,然后开始爆炸....我知道如果我将互斥锁超时设置为60000ms,它会没问题,但为什么我只想要250ms的过程呢?另外,为什么它从threadid#1跳转到threadid#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; 
}

enter image description here

2 个答案:

答案 0 :(得分:1)

只需将 MUTEX_TIMEOUT 替换为 INFINITE 或更大的数字。

由于操作系统管理它们,您将不知道哪个线程在某个时间可以工作。不要担心thread_25更早发挥作用。如果您希望线程按创建顺序运行,则应手动管理它们。

答案 1 :(得分:1)

你几乎在同一时间创建了25个线程,如果每个线程都使用互斥量约250毫秒,然后每个线程背靠背运行,所有线程处理的总时间将是250毫秒* 25 = 6250毫秒。考虑到这一点,你的一些线程肯定会超时获取互斥锁,因为你只等待2000毫秒。实际上,看起来在处理了8个线程之后,剩余的超时时间(从250ms * 8 = 2000ms开始就不会出乎意料)。

至于线程#25在线程#1之后获取互斥锁,我不相信在等待和获取互斥锁方面有任何保证的顺序。运行它多次,每次运行都可能会得到不同的订单。