为什么这个线程不能一直读取管道中的所有数据?

时间:2010-06-30 18:31:43

标签: c++ windows multithreading winapi

一般来说这有什么问题吗?

CallingFunction()
{
   CreatePipe()
   CreateMutex()
   CreateThread( ThreadFunction )

   while(there is data left to send)
   {
      WriteFile(send data in 256 byte chunks)
   }

   WaitForSingleobject() //don't return until ReadThread is done

   return 0;
 }

ThreadFunction()
{
   WaitForSinglObject()

   while(bytesRead != totalBytestoReadFileSize)
   {
      ReadfromPipe(in chunks)
      update bytesRead++
   }

   ReleaseMutex()

   return 0;
 }

在调用函数结束之前 -

FileSize:232016
BytesWrittenToPipe:232016
BytesReadFromPipe:231946或232012或231840 - >为什么不一致?

1 个答案:

答案 0 :(得分:1)

期望我们从伪代码调试这样的问题是不现实的。使用FlushFileBuffers确保写入管道中的所有数据。