So, I've been trying to figure out, how to wait for data from a C++ stringstream (for instance), without being constantly checking if data is there, which is quite CPU consuming.
I'm perfectly able to read, for instance, from a serial device, and lock the process while no data arrives, but unfortunately I haven't been able to figure how to do that with C++ streams.
I'm sure I'm missing something, since cin does exactly that, i.e., waits for the return key to step out from istream reading, but how does it do it?
Thanks in advance for any light on the subject.
std::getline
? – Inconsequentstreambuf
s don't work the same. (The various streams just forward to astreambuf
.) In particular,filebuf
has the notion ofopen
, and depends on the OS to tell it when there are no more characters to be read. This concept is absent instringbuf
. So it can't wait for the "output" side to be closed to declare end of file on the input. – Pelagiancin
,cout
, andfistream
andfostream
all do work the same and so do their streambufs. They work the same, because the underlying system calls (or rather ANSI C library calls that file streams normally work on) are the same for those two cases. And as long as a device can be used with ANSI C functions (on POSIX, anything can, on Windows you are out of luck and have to write the streambuf yourself or get one somewhere), it can be used and will work out of the box. – Dominatecin
,cout
,ifstream
andofstream
work the same, because they all usefilebuf
. The OP asks aboutstringstream
, which uses astringbuf
. A lot of the time, of course, you'll be using some otherstreambuf
. (And I've not noticed a great deal of difference between Unix and Windows in this respect. In both,filebuf
are usually normally named files, but you can also open them on special devices which behave differently.) – Pelagian