当前位置:网站首页>C language: timer principle
C language: timer principle
2022-06-13 09:12:00 【Caixinzhi】
First , Need to know ,C The timer of the language delays the running result , among , Timing unit: bits per millisecond .
Timer , seeing the name of a thing one thinks of its function , It will take time , therefore , To get time, we need to use time library , So you need to reference the header file <time.h>
To make a timer , First, you need to set the static variables t1 and t2, Because only when setting static variables ,t1 and t2 The value of will not become the original initialized value every time the loop is executed , We need to fix it , The initial value executed in the next loop is the result value after the previous execution , therefore , It is natural to consider the use of static variables .
Next , Is to set a condition in the loop , For example, the interval between changes you want to make is 500 In milliseconds , You can write... In a conditional statement t2 - t1 > 500, such , It can be well expressed . Then write the code or custom function you want the program to execute in the statement block of the conditional statement , And will t2 Assign a value to t1. If the conditional statement is not satisfied , Or after executing the conditional statement block , Then assign the current time to t2, therefore , And that's where it comes in clock Function to get the current time . such , You can simply make the most basic timer , The specific code is as follows :
while (1)
{
static DWORD t1, t2;
if (t2 - t1 > 500)
{
Timer();
t1 = t2;
}
t2 = clock();
}
One thing to note here , Static variables t1 and t2 Can be assigned an initial value , But because you can't use it here , So there is no assignment to them , Equivalent to t1 and t2 be equal to 0, Because the initial values of static variables are 0.
above , It is the simplest timer , Next , We are going to upgrade this timer . We can encapsulate this timer into a function , The advantage of this is , When timers are used in other functions to delay , You can call this function directly to operate quickly and efficiently . The following is the code after encapsulating the timer :
void Add(int ms)
{
static DWORD t1, t2;
if (t2 - t1 > ms)
{
Timer();
t1 = t2;
}
t2 = clock();
}
here , It is easy to find , We just need to enter the number of seconds we want to delay where we call this function , Call the function directly .
But here , There's a problem , When you want to call this timer function in two or more different places , Through debugging, it will be checked that the second timer called is useless , Unable to achieve the function you want . So how to solve this problem ?
Here is a method that I prefer , It is to pass in another parameter to determine which one wants to call the timer function . therefore , When we get here , We can simplify two static variables into one , To continue the implementation , See the following figure for the specific package timer function :
int Add(int ms, int id)
{
static DWORD t[10];
if (clock() - t[id] > ms)
{
t[id] = clock();
return 1;
}
return 0;
}
If the conditions are met, execute and return 1, Go back if you don't agree 0. then , You can write a where you want to call if Sentence to judge 0 perhaps 1,1 You can continue to execute , So the code at the call can be written as :
if (Add(500, 0) == 1) // Call the timer function
{
Timer();
}
For example, it is written as above , first id It can be transmitted 0 In the past , The second one can be passed 1……, And so on , Because what I write here is t[10], So at most, you can pass in 10 Number , It can also be set according to requirements t And so on . If it is equal to 1, You can execute Timer function , It has a good function of delay .
There are many extensions to the timer principle , It is also widely used in many situations , Here is just a simple help to understand , Further systematic study is needed to master deeper contents .
边栏推荐
- A very detailed blog about the implementation of bilinear interpolation by opencv
- Yolov5 face video stream
- output. Interpretation of topk() function
- 网络安全漏洞分析之重定向漏洞分析
- 20211108 det(AB)=det(A)det(B)
- 20211018 some special matrices
- JUC atomic integer
- 202012 CCF test questions
- 20211108 A转置乘A是正定矩阵吗?A转置乘A是正定矩阵的充分必要条件是什么?
- 你不知道的stringstream的用法
猜你喜欢
Collection of garbled code problems in idea development environment
C/s model and P2P model
JUC原子引用与ABA问题
Final principle
Detailed explanation of C language callback function
Drill down to protobuf - Introduction
【安全】零基礎如何從0到1逆襲成為安全工程師
QObject::connect: Cannot queue arguments of type ‘QTextCursor‘ (Make sure ‘QTextCursor‘ is registere
Qvector shallow copy performance test
Longadder of the source code of JUC atomic accumulator
随机推荐
Tutorial (5.0) 01 Product introduction and installation * fortiedr * Fortinet network security expert NSE 5
[QNX hypervisor 2.2 user manual] 4.5.1 build QNX guest
Redis fuzzy query batch deletion
【QNX Hypervisor 2.2 用户手册】4.5.1 构建QNX Guest
类的加载概述
Necessary and sufficient conditions for diagonalization of 20211115 matrix; The full rank matrix does not necessarily have n linearly independent eigenvectors; Symmetric matrices must be diagonalized
【 sécurité 】 comment devenir ingénieur de sécurité de 0 à 1 contre - attaque pour la Fondation zéro
Batch read all voice files under the folder
A very detailed blog about the implementation of bilinear interpolation by opencv
Pytorch same structure different parameter name model loading weight
Tutorial (5.0) 04 Fortint cloud services and scripts * fortiedr * Fortinet network security expert NSE 5
Pytorch model tuning - only some layers of the pre training model are loaded
Figure introduction to database neo4j
线上调试工具Arthas高级
JUC Unsafe
Yolov5 model evaluation
JUC atomic accumulator
You don't know the usage of stringstream
Collection of garbled code problems in idea development environment
Neo4j環境搭建