c++ - C++ 从另一个函数退出函数
问题描述
我为具有读取功能的串行链接创建了一个类。我使用 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# - 如何使用目标视图模型中收集的数据从目标视图模型返回到源视图模型并在源视图模型中调用方法
- r - 使用 R 检查多个列中的值是否等于列表中的任何元素
- python - Python POST 请求显示“不可接受!”
- ruby-on-rails - 将多个数组 + 变量组合成单个 JSON 对象
- sql - 更新 SQL 中的多个值
- java - Java - 递归双阶乘算法
- azure - 标准连接器转向高级连接器。逻辑应用程序是否也受到影响
- file - 请求正文中的动态文件
- sql - How to send a query request to SQL Server via tcp ip (with a general tool, such as Packet Sender) on the same PC?
- git - VSCode:区别“创建分支”和“创建分支”