I do not believe that there is a clean way to do this, it is a portable solution in C ++. It is best to use poll or select on * nix and WaitForSingleObject or WaitForMultipleObjects systems on Windows.
You can do this transparently by creating a streambuffer proxy class that forwards calls to the real streambuffer object. This will allow you to call the appropriate wait function before doing the actual read. It might look something like this ...
class MyStreamBuffer : public std::basic_streambuf<char> { public: MyStreamBuffer(std::fstream& streamBuffer, int timeoutValue) : timeoutValue_(timeoutvalue), streamBuffer_(streamBuffer) { } protected: virtual std::streamsize xsgetn( char_type* s, std::streamsize count ) { if(!wait(timeoutValue_)) { return 0; } return streamBuffer_.xsgetn(s, count); } private: bool wait() const {
You will need to do this with every call. This may be a little tedious, but it will provide a transparent solution for providing timeouts, even if they cannot be explicitly supported in client code.
source share