title: Exit application while stdin blocking on windows
body: <p>I have an application, which reads data from standard input using getline() in a thread. I want to close the application from the main thread, while getline still block the other thread. How can this be achieved?</p>
  
  <p>I don't want to force the users to have to press ctrl-Z to close stdin and the application.</p>
  
  <p>I have tried so far with my complier settings (RuntimeLibrary=/MT) on Windows 8.1 64bit, v120 platform toolset:</p>
  
  <ul>
  <li>freopen stdin, but it is blocked by internal lock</li>
  <li>destructing the thread, which calls abort()</li>
  <li>putback an Eof, line end to std::cin, which also blocked</li>
  </ul>
  
  <p><strong>* Update *</strong></p>
  
  <ul>
  <li>detach() does not work, exit() blocked by a lock </li>
  <li>winapi TerminatThread() calls abort()</li>
  <li>winapi CloseHandle(GetStdHandle(STD_INPUT_HANDLE)) hangs</li>
  <li>calling TerminateProcess() - works, but I would like to exit gracefully</li>
  </ul>
  
  <p><strong>* Update 2: Solution *</strong></p>
  
  <ul>
  <li>WriteConsoleInput() can make std::getline() return from blocking read. This works with any msvc runtime libray. For code of working solution see accepted answer.</li>
  </ul>
  
  <hr>
  
  <p>Example code showing the problem:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;thread&gt;
  #include &lt;string&gt;
  #include &lt;chrono&gt;
  
  int main(int argc, char *argv[])
  {
      bool stop = false;
      std::thread *t = new std::thread([&amp;]{
          std::string line;
          while (!stop &amp;&amp; std::getline(std::cin, line, '\n')) {
              std::cout &lt;&lt; line;
          }
      });
  
      std::this_thread::sleep_for(std::chrono::seconds(1));
  
      stop = true;
      // how to stop thread or make getline to return here?
  
      return 0;
  }
  </code></pre>