1

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.

14
  • wait on stdin_handle is initial error Commented Jan 25, 2024 at 13:23
  • Could you please explain your answer @RbMm? Why would that be an error? Is there a relevant part of the official online documentation addressing this issue? Commented Jan 25, 2024 at 13:33
  • 1
    in case input is redirected - wait on file is error always. and for what you wait at all ? you can simply use ReadFile in asynchronous mode as example Commented Jan 25, 2024 at 13:34
  • 1
    yes, will be no formal error, because WaitForSingleObject let wait on file handle too, however this is wrong by sense Commented Jan 25, 2024 at 14:43
  • 1
    @dde that doesn't change what I said. The reader thread could wait on the console input and a CreateEvent object at the same time, and another thread could signal the event when the reader should stop Commented Jan 26, 2024 at 15:18

1 Answer 1

0

This is not an answer: just posting this as an example of what I was saying in my comment. Could you do something like this?

#include <iostream>
#include <atomic>
#include <thread>
#include <chrono>
using namespace std::chrono_literals;

struct Info
{
    std::atomic<bool> running;
};

void one_seconder(Info* info)
{
    for (int ii = 0; info->running; ++ii)
    {
        std::cout << ii << std::endl;
        std::this_thread::sleep_for(1000ms);
    }
    std::cout << "Out of thread" << std::endl;
}
int main()
{
    Info info;
    char ch;

    // Start thread
    info.running = true;
    std::thread tobj(one_seconder, &info);

    std::cout << "Type any character and hit return" << std::endl;
    // Alternatively #include <conio.h> and use _getch();
    std::cin >> ch;
    info.running = false;
    std::cout << "Terminating" << std::endl;

    // Wait for thread to terminate
    tobj.join();

    std::cout << "Exit" << std::endl;
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.