timeout - How to read for X seconds maximum in C++? -
i want program wait read in fifo, if read
(i use std::fstream
) lasts more 5 seconds, want exit.
is possible or have use alarm
absolutely?
thank you.
i not believe there clean way accomplish portable c++ solution. best option use poll
or select
on *nix based systems , waitforsingleobject
or waitformultipleobjects
on windows.
you can transparently creating proxy streambuffer
class forwards calls real streambuffer
object. allow call appropriate wait
function before doing actual read. might 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 { // not entirely complete idea return (wait_object_0 == waitforsingleobject(...)); } const int timeoutvalue_; std::fstream& streambuffer_; };
you need on every call through. might little tedious provide transparent solution providing timeouts might not explicitly supported in client code.
Comments
Post a Comment