如何在c++中读取最多X秒

How to read for X seconds maximum in C++?

本文关键字:读取 c++      更新时间:2023-10-16

我希望我的程序等待在FIFO中读取的东西,但如果read(我使用std::fstream)持续超过5秒,我希望它退出。

是可能的还是我必须使用alarm绝对?

谢谢。

我不相信有一种干净的方法可以完成这一任务,而这种方法只能是可移植的c++解决方案。您最好的选择是在基于*nix的系统上使用pollselect,在Windows上使用WaitForSingleObjectWaitForMultipleObjects

您可以通过创建代理streambuffer类来转发对真实streambuffer对象的调用,从而透明地完成此操作。这将允许您在执行实际读取之前调用适当的wait函数。它可能看起来像这样…

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 but you get the idea
         return (WAIT_OBJECT_0 == WaitForSingleObject(...));
     }
    const int       timeoutValue_;
    std::fstream&   streamBuffer_;
};

您需要在每次通话时都这样做。这可能会有点乏味,但会为提供超时提供一个透明的解决方案,即使在客户机代码中可能没有显式支持超时。

对于我解决问题的方式感兴趣的人来说,这是我从流中读取的函数。我最终不能使用std::fstream,所以我用C系统调用代替了它。

std::string
NamedPipe::readForSeconds(int seconds)
{
    fd_set              readfs;
    struct timeval      t = { seconds, 0 };
    FD_ZERO(&readfs);
    FD_SET(this->_stream, &readfs);
    if (select(this->_stream + 1, &readfs, NULL, NULL, &t) < 0)
        throw std::runtime_error("Invalid select");
    if (FD_ISSET(this->_stream, &readfs))
        return this->read();
    throw NamedPipe::timeoutException();
}