c - 是否可以仅在 C 中的一定时间后执行条件
问题描述
我想知道是否只有当条件在一定时间内为真时才能执行一段代码。
例如 :
if(position_goal_reached == 1) //but this should be true for 1 second)
{do something;}
我不认为 C 能够做到这一点,但我想确定一下。
解决方案
我建议您使用time()
,sleep()
或.clock()
usleep()
注 1:记得包含#include <unistd.h>
或/和#include <time.h>
注意 2:如果您使用的是类 Unix 系统,那么 clock() 会出现一些问题。
我想你想做下一个:
#include <stdio.h>
#include <time.h>
int main(void)
{
clock_t start;
start = clock();
int milliseconds = 1000; // The amount of milliseconds that the conditional must take (This case 1 second)
if(position_goal_reached == 1)
{ start_again_if:
do something; //What you want to do must be here
if ((int)(clock() - start) < milliseconds ) goto start_again_if;
}
}
我希望这会有所帮助。
推荐阅读
- string - Powershell:从txt中提取几个字符串并从中创建表
- wordpress - 如果购物车中存在两种不同的运输类别,则仅显示 1 个特定的运输类别
- php - Codeigniter 3 - do_upload 中无法识别文件
- angular - owl-date-time 和 http 角度同步
- objective-c - 未定义符号:_OBJC_CLASS_$_FIRApp
- prolog - 扩展统一,SICStus 风格
- rust - 如何从 actix SyncContext 向另一个参与者发送消息?
- tensorflow - 5000 epoch (LSTM) 后学习的巨大峰值
- python - 如何在略有不同的键上合并两个熊猫数据框
- r - 变量类是 data.table 中的列表