Hi,

I've attached simple cpp program which demonstrates the problem.
The program works fine from cmd.exe, but fails when started from cygwin.

The program does the following:
- The main thread creates a helper thread which reads stdin and prints
the data read.
- stdin is read via ReadFile() Win32 API function.
- At the end of the main function, the main thread calls CloseHandle()
func for the stdin. It causes the ReadFile() function to return, and
the helper thread to finish. From cmd the program works as described.
- Under Cygwin  the program works differently. The helper thread is
not finished, because it doesn't exit from the ReadFile() func. The
main thread waits for the helper thread's completion during 1 sec,
then it calls TerminateThread() Win32 API func.

When I start the program from cmd, I see the following output:
>read_stdin_example.exe
Stdin_Reader_Thread has been completed

When I start the program from Cygwin, I see:
$ ./read_stdin_example.exe
Stdin_Reader_Thread has NOT been completed. So, it will be terminated.

I believe that this information would be enough to investigate and fix
the problem.
If you need more information or have questions, feel free to contact me.

Have a nice day!
~Alexey
#include <stdio.h>
#include <windows.h>

#define MY_BUF_SIZE 1024

DWORD WINAPI read_stdin(LPVOID lpParameter)
{
    BOOL bResult = FALSE;
    char szBuf[MY_BUF_SIZE] = "";
    int nIndex = 0;
    DWORD nNumOfBytesRead = 0;

    HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
    if (hStdin == NULL || hStdin == INVALID_HANDLE_VALUE)
    {
        printf("GetStdHandle failed for STD_INPUT_HANDLE\n");
        exit(2);
    }

    nIndex = 0;
    for (;;)
    {
        nNumOfBytesRead = 0;
        szBuf[nIndex] = '\0';
        bResult = ReadFile(hStdin, &szBuf[nIndex], 1, &nNumOfBytesRead, NULL);

        if (bResult)
        {
            if (nNumOfBytesRead == 0)
            {
                // End of the file was reached

                // Print the data read if they exist
                if (nIndex > 0)
                {
                    printf("Input data = \"%s\"\n", szBuf);
                }

                break;
            }

            if ( (szBuf[nIndex] == '\n') || (nIndex == MY_BUF_SIZE - 1) )
            {
                // End of the input has been detected
                // Print the input data
                printf("Input data = \"%s\"\n", szBuf);

                // Reset the index back to the beginning of the input buffer
                nIndex = 0;
            }
            else
            {
                // Increment the index and continue the reading
                ++nIndex;
            }
        }
        else
        {
            DWORD dwErrorCode = GetLastError();

            printf("ReadFile failed ===== LastErrorCode = %d\n", dwErrorCode);
            exit(3);
        }

    } // infinite for cycle

    return 0;
} // read_stdin

int main(int argc, char *argv[])
{
    BOOL bResult = FALSE;

    DWORD dwThreadID = 0;
    HANDLE hStdinReaderThread = CreateThread(
        NULL, 0, (LPTHREAD_START_ROUTINE)read_stdin, NULL, 0, &dwThreadID);
    if (!hStdinReaderThread)
    {
        printf("CreateThread failed\n");
        exit(1);
    }

    // Simulate work in the main thread
    Sleep(1000);

    // Close the stdin => the stdin reader thread should exit in that case
    CloseHandle(GetStdHandle(STD_INPUT_HANDLE));
    if (WaitForSingleObject(hStdinReaderThread, 1000) != WAIT_OBJECT_0)
    {
        printf("Stdin_Reader_Thread has NOT been completed. So, it will be 
terminated.\n");
        TerminateThread(hStdinReaderThread, 1);
    }
    else
    {
        printf("Stdin_Reader_Thread has been completed\n");
    }
    CloseHandle(hStdinReaderThread);

    return 0;
} // main
--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

Reply via email to