I have threads in my program and I want to put character into stream and read it in another thread, but after std::cin.putback() I need to write something from keyboard to “wake up” std::cin in function main. Can I do something to read automatically?
Share
That’s not how streams work. The
std::cinreads data that come from outside your program to standard input and theputbackonly allows keeping a character that you actually just read back to the buffer for re-parsing next time you invokeoperator>>(orgetorgetlineor other read method).If you want to communicate between threads, you should use a message queue from some threading library, e.g. Boost provides a decent portable one.
It is not possible to use streams, at least those provided by standard library, because
stringstreamis not thread-safe andfistream/fostreamcan’t be created from raw file handle, so you can’t combine them with POSIXpipefunction. It would be possible to wrap a message queue in a stream (and boost gives you enough tools to do it), but the raw message queue API will probably be suitable.