如何在c++中读取最多X秒
How to read for X seconds maximum in C++?
我希望我的程序等待在FIFO中读取的东西,但如果read
(我使用std::fstream
)持续超过5秒,我希望它退出。
是可能的还是我必须使用alarm
绝对?
谢谢。
我不相信有一种干净的方法可以完成这一任务,而这种方法只能是可移植的c++解决方案。您最好的选择是在基于*nix的系统上使用poll
或select
,在Windows上使用WaitForSingleObject
或WaitForMultipleObjects
。
您可以通过创建代理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();
}
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- 使用新行和不使用新行读取文件
- 读取文件并输入到矢量中
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 无法找到/读取配置文件.conf-FileIOException
- 如何使用Luacneneneba API正确读取字符串和表参数
- C++将文本文件中的数据读取到结构数组中
- 正在将csv文件读取为双精度矢量
- 为什么 sscanf 无法从一个字符串中读取uint64_t和字符?
- 为什么在读取文件大小时文件IO速度会发生变化
- 正在读取二进制文件(is_open)
- 如何在c++中从文本文件中逐行读取整数
- SSH通过/sbin/SSH无法读取RSA密钥文件(从控制台运行)
- 独立读取-修改-写入顺序
- 从文本文件中读取时钟时间和事件时间并进行处理
- 如何从文本文件中读取值和数组
- 为什么文件名被设置为一个点,而不是在读取矢量中的文件名时
- 一种在C++中读取TXT配置文件的简单方法