c++ - 用箭头代码重构无限循环
问题描述
我有一个具有一定周期(延迟)的无限循环。我有以下结构:
// define the i32threshold of time
// set i32counter to 0
while(true)
{
if(bExecuteEnable)
{
if(bConnectionSet)
{
if(++i32counter >= i32threshold)
{
// Do the job
counter = 0;
}
}
}
delay(1); // 1 ms of delay introduces the period
}
if
除了将所有条件合并到单个语句中之外,您是否有任何建议来重构这个无限循环?
解决方案
重写它看起来简单了很多:
while (!bExecuteEnable && !bConnectionSet && ++i32counter < i32threshold) {
delay(1); // 1 ms of delay introduces the period
}
counter = 0;
当然希望i32threshold
小于您的最大值,i32counter
否则您将永远溢出并循环。
推荐阅读
- node.js - 如何登录保护节点/反应中的静态 html 页面?
- python - 不确定为什么当它是一种方法时它是不可调用的
- python - 每个数组中的元素数量如何最终少于其父数组(数组数组)中的元素数量
- php - 在php中提交表单时页面挂起
- powerbi - Power BI - 带有过滤器的 rankx
- asp.net-mvc - 如何将毫秒转换为 hh mm ss 格式并将其保存在 Db 中
- reactjs - 无法读取未定义反应挂钩的属性“位置”
- sqlite - Microsoft Access 与 SQLite 后端 #DELETED 记录问题 - 另一种解决方案
- r - 如何在具有多个 data.frame 的列表中进一步过滤变量
- java - ClassGraph,如何过滤类路径的内容