I am porting some Unix code to Windows which redirects stderr and stdout to a pipe that I created and there's a thread which reads from this pipe and then sends the output to the debug console. This works fine on Unix, but I can't get it working on Windows. The problem occurs when the read side of the pipe is closed. Instead of writing EOF to the pipe which would cause the thread to quit, it deadlocks. Why?

One workaround is to skip the call to close, which worries me a little, but since my process is short lived, maybe it isn't a big deal? 

Here is sample code which illustrates the problem... I'm using VS 2010: 

```
#include <cstdio>
#include <tchar.h>
#include <iostream>
#include <vector>
#include <fcntl.h> 
#include <Windows.h>
#include <io.h>
#define posix_open _open
#define posix_read _read
#define posix_write _write
#define posix_pipe( fds ) _pipe( fds, 8096, _O_BINARY)
#define posix_close _close
#define posix_dup _dup
#define posix_dup2 _dup2
#define posix_fileno _fileno

using namespace std;

static const int PIPE_READ  = 0;
static const int PIPE_WRITE = 1;

DWORD __stdcall PipeReaderFunc(void* readFd)  
{  
    int pipeFd = *((int*)readFd);
    vector< char > buffer(8096);
    while( posix_read(pipeFd, &buffer[0], buffer.size() ) != 0 )
    {
        OutputDebugString( &buffer[0] );
    }
    return 0;
} 

void test() 
{
    int pipefd[2] = {-1,-1};
    if( posix_pipe( pipefd ) < 0 )
    { throw std::exception( "Failed to initialize pipe." );}

    int stdoutOrig = posix_dup( _fileno(stdout) );
    int stderrOrig = posix_dup( _fileno(stderr) );
    if( -1 == posix_dup2( pipefd[PIPE_WRITE], posix_fileno(stdout) ) ) // closes stdout
    {throw exception( "Failed to dup stdout fd." );}

    if( -1 == posix_dup2( pipefd[PIPE_WRITE], posix_fileno(stderr) ) ) // closes stderr
    {throw exception( "Failed to dup stderr fd." );}

    HANDLE hThread = CreateThread( NULL, 0, PipeReaderFunc, &pipefd[PIPE_READ], 0, NULL); 
    if( NULL == hThread )
    {throw exception("Failed to create thread");}

    cout << "This should go to the debug console" << endl;
    Sleep(1000); // Give time for the thread to read from the pipe

    posix_close( stdoutOrig ); 
    posix_close( stderrOrig ); 
    posix_close( pipefd[PIPE_WRITE] );

    // Deadlock occurs on this line
    posix_close( pipefd[PIPE_READ] );

    // This is commented out because it has no effect right now.
    //WaitForSingleObject( hThread, INFINITE );
}

int _tmain(int argc, _TCHAR* argv[])
{
    try 
    { test(); } 
    catch( exception& ex ) 
    { cerr << ex.what() << endl; }
    return 0;
}

```

Thanks for any ideas on how to resolve this!!