有一个1MB的管道:

if (0 == CreatePipe(&hRead,&hWrite,0,1024*1024))
{
printf("CreatePipe failed\n");
return success;
}

一次发送4000字节(bytesReq = 4000)
while ((bytesReq = (FileSize - offset)) != 0)
{


//Send data to Decoder.cpp thread, converting to human readable CSV
        if ( (0 == WriteFile(hWrite,
                               readBuff,
                               bytesReq,
                               &bytesWritten,
                               0) ) ||
                               (bytesWritten != bytesReq) )
        {
             printf("WriteFile failed error = %d\n",GetLastError());
             break;
        }

}

Only 4 bytes at a time being read in at another thread, on other end of pipe.

当我将管道缩小时,发送和读取的总时间大大减少了。

将管道尺寸更改为–
1024 * 1024 = 2分钟(原始尺寸)
1024 * 512 = 1分钟47秒
10,000 = 1分钟33秒
低于10k,1分钟33秒

怎么会这样?

最佳答案

等待更少。

如果管道缓冲区太大,那么一个进程将写入所有数据,并在第二个进程甚至开始之前将其关闭。

当管道太大时,将顺序执行这些过程。

07-24 09:36
查看更多