如何在C中使用计时器? [英] How to use timer in C?

查看:33
本文介绍了如何在C中使用计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C中使用定时器的方法是什么?我需要等到 500 毫秒才能找到工作.请提及完成这项工作的任何好方法.我使用了 sleep(3); 但是这个方法在这段时间内没有做任何工作.我有一些东西会尝试直到那个时候得到任何输入.

What is the method to use a timer in C? I need to wait until 500 ms for a job. Please mention any good way to do this job. I used sleep(3); But this method does not do any work in that time duration. I have something that will try until that time to get any input.

推荐答案

您可以使用 time_t 结构和 clock() 来自 time.h 的函数.

You can use a time_t struct and clock() function from time.h.

使用 clock() 将开始时间存储在 time_t 结构体中,并通过比较存储的时间和当前时间之间的差异来检查经过的时间.

Store the start time in a time_t struct by using clock() and check the elapsed time by comparing the difference between stored time and current time.

这篇关于如何在C中使用计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆