Cross-platform (linux/Win32) nonblocking C++ IO on stdin/stdout/stderr

前端 未结 3 1755
南笙
南笙 2021-02-14 12:14

I\'m trying to find the best solution for nonblocking IO via stdin/stdout with the following characteristics:

  • As long as there is enough data, read in n-s
相关标签:
3条回答
  • 2021-02-14 12:41

    I used the threads and platform specific code. See my answer to another question. I was able to put the OS-specific stuff in inputAvailable() (Linux uses select, Windows just returns true). I could then use WaitForSingleObject() with a timeout on Windows to try to let the thread complete, then TerminateThread() to kill it. Very ugly, but the team didn't want to use this bit of boost.

    0 讨论(0)
  • 2021-02-14 12:55

    Maybe boost::asio can be of use for you?

    0 讨论(0)
  • 2021-02-14 12:55

    I did something similar to jwhitlock ... I ended up with a StdinDataIO class that wraps around the appropriate OS-specific implementation(*) so that the rest of my program can select() on the file descriptor StdinDataIO provides, remaining blissfully ignorant of Windows' limitations regarding stdin. Have a look here and here if you like, the code is all open-source/BSD-licensed.

    (*) the implementation is a simple pass-through for Linux/MacOSX, and in Windows it's a rather complex process of setting up a child thread to read from stdin and send the data it receives over a socket back to the main thread... not very elegant, but it works.

    0 讨论(0)
提交回复
热议问题