【发布时间】:2019-03-24 01:35:11
【问题描述】:
我为串行链接创建了一个具有读取功能的类。 我使用 boost::asio::read 从串行链路读取数据。但是读取函数会无限等待,直到接收到一个字节。
如果最长等待时间已过,我想创建一个线程来停止读取功能(因为系统中似乎存在故障)。
是否可以从另一个函数中退出 C++ 中的函数?或者取消其他函数的读取函数调用?
std::string SerialLink::read(const int maxTime) {
std::string data;
std::vector < uint8_t > buf;
const int readSize = 1;
try {
buf.resize(readSize);
//boost::asio::read waits until a byte has been received
boost::asio::read(port_, boost::asio::buffer(buf, readSize));
data = buf.front();
}
catch (const std::exception & e) {
std::cerr << "SerialLink ERROR: " << e.what() << "\n";
return -1;
}
return data();
}
void threadTime() {
//This function will keep track of the time and if maxTime has passed, the read function/function call must be cancelled and return -1 if possible
}
【问题讨论】:
标签: c++ function call exit infinite