为什么线程同步不起作用?

Raf*_*fał 4 c++ multithreading windows-ce

我编写了一个多线程程序,其中三个线程试图将文本保存到同一个文件中.我应用了关键部分.并且在Windows 7下运行完美,但在CE 6.0中不同步,即每个线程同时尝试保存:

它现在有效!!! 谢谢大家的帮助!

模拟器

内核跟踪器

关键部分:

InitializeCriticalSection(&CriticalSection);

// Create worker threads
for( i=0; i < THREADCOUNT; i++ )
{
    aThread[i] = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE) WriteToFile, NULL, 0, &ThreadID);

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

// Wait for all threads to terminate
for( i=0; i < THREADCOUNT; i++ )
{
    WaitResult = WaitForSingleObject(aThread[i], INFINITE);

    switch(WaitResult)
    {
        case WAIT_OBJECT_0:
            printf("Thread %d has terminated...\n", i);
         break;

         // Time out
        case WAIT_TIMEOUT:
            printf("The waiting is timed out...\n");
            break;

        // Return value is invalid.
        default:
            printf("Waiting failed, error %d...\n", GetLastError());
            ExitProcess(0);
    }
}

// Close thread handles
for( i=0; i < THREADCOUNT; i++ )
    CloseHandle(aThread[i]);

// Release resources used by the critical section object.
DeleteCriticalSection(&CriticalSection);
Run Code Online (Sandbox Code Playgroud)

线程调用的函数:

DWORD WINAPI WriteToFile( LPVOID lpParam )
{ 
// lpParam not used in this example
UNREFERENCED_PARAMETER(lpParam);

DWORD dwCount=1, dwWaitResult; 

HANDLE hFile; 
char DataBuffer[30];
DWORD dwBytesToWrite;
DWORD dwBytesWritten;

// Request ownership of the critical section.
EnterCriticalSection(&CriticalSection);

    // Write to the file
    printf("Thread %d writing to file...\n", GetCurrentThreadId());

    hFile = CreateFile(TEXT("file.txt"), GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); 

    SetFilePointer(hFile, 0, NULL, FILE_END);

    while( dwCount <= 3 )
    {
        sprintf(DataBuffer, "Theard %d writing %d\n", GetCurrentThreadId(), dwCount);
        dwBytesToWrite = (DWORD)strlen(DataBuffer);

        WriteFile( hFile, DataBuffer, dwBytesToWrite, &dwBytesWritten, NULL);

            printf("Theard %d wrote %d successfully.\n", GetCurrentThreadId(), dwCount);
            }
        }

        dwCount++;
    }

CloseHandle(hFile);             

// Release ownership of the critical section.
LeaveCriticalSection(&CriticalSection);

return TRUE; 
}
Run Code Online (Sandbox Code Playgroud)

Ant*_*ams 10

问题是要传递TRUEfWaitAll标志WaitForMultipleObjects.在Windows CE上,不支持此操作:MSDN上文档说明此标志必须是FALSE.WaitForMultipleObjects因此不是等待,而是返回错误,但您没有检查返回代码.因此,主线程直接通过,关闭句柄并删除关键部分,而"工作"线程仍在运行.一旦DeleteCriticalSection被调用,关键部分"不能再用于同步",因此EnterCriticalSection调用可能不再阻止,并且您最终得到了此处的方案.

在Windows 7上,一切正常,因为WaitForMultipleObjects调用确实等待所有线程完成.

而不是使用WaitForMultipleObjects,只需WaitForSingleObject在循环中使用依次等待每个线程.