This problem occurs in a multi-threaded application (two threads). It seems to occurs on Windows only in a very specific configuration - more details on that later. The program is written in C and uses the pthreads library. It is is essentially a command-line interpreter.
The main thread spawns a "reader" thread that executes a function that is responsible for reading user input text line by line from the standard input and queuing the line read.
The main thread dequeues the line, interpret and executes the corresponding commands.
The problem is in the "reader" thread.
HANDLE stdin_handle;
DWORD wait_result;
stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
if (stdin_handle == INVALID_HANDLE_VALUE) {
return ERROR_INVALID_HANDLE_VALUE;
}
if (stdin_handle == NULL) {
return ERROR_NULL_HANDLE_VALUE;
}
do {
wait_result = WaitForSingleObject(stdin_handle, 100);
/* omitted: verify some condition here, break from loop if satisfied */
}
while(wait_result == WAIT_TIMEOUT);
The calls to WaitForSingleObject "reader" thread always return WAIT_TIMEOUT when the program is run in some specific configurations.
Configuration where the problem occurs:
- the program is run in git bash, without redirection (input direct from keyboard)
Configurations where the does not problem occur:
- the program is run in git bash under Windows, with stdin redirected from a file
- the program is run within gdb, without redirection (input direct from keyboard)
- the program is run in CMD or Powershell, without redirection (input direct from keyboard)
I have read the synchapi.h API documentation but could not find the information I need to solve this issue.
ReadFilein asynchronous mode as example