Dev C%2b%2b Delay Function
The delay function is used to suspend execution of a program for a particular time. Declaration: void delay (unsigned int); Here unsigned int is the number of milliseconds (remember 1 second = 1000 milliseconds). To use delay function in your program you should include the 'dos.h' header file which is not a part of standard C library. The time function takes a pointer to timet object as its argument and returns the current calendar time as a value of type timet. If arg is not a null pointer, the returned value is also stored in the object pointed to by arg. Delay is latent function. The long story short there is some magic behind scenes, which create proxy objects, which are reponsible for keeping state of delay (or any other). You can't use it in C, because latent blueprint functions are very specific to how blueprint works. Parameters and Arguments. Information can be passed to functions as a parameter. Parameters act as variables inside the function. Parameters are specified after the function name, inside the parentheses. So anyone know how to give delay time in dev c? Vlad from moscow. You can use std::difftime function declared in For example. 1 2 3: std::timet start.
Some languages, such as JavaScript or Visual Basic, offer the feature of a timer, that is to say an object that calls some code at defined intervals. At the time of this writing (C++17) and to my knowledge, C++ doesn’t offer such a feature.
A library called timercpp
, that replicates in C++ this handy functionality of other languages, was on the front page of HN. It has a nice interface that allows for expressive code, however I don’t think it is quite ready for being used in production.
If it is not production-ready, why talk about it then? For two reasons:
- its implementation is instructive to learn about C++ standard library’s basic usages of threads,
- the reasons why it should maybe not be used in production are also instructive.
I learned several things when looking at this library and the discussion around it, so I figured maybe other people could find this instructive too.
timercpp
uses an interface inspired from JavaScript, by implementing a setTimeout and a setInterval functions. This leads to a nice interface:
setInterval
allows to run the code of the same function repeatedly, at a given interval. In the above example, the function is a lambda that displays “Hey. After each 1s…”. And setTimeout
plans one execution of a function in a given amount of time, here printing “Hey. After 5.2s. But I will stop the timer!” and stopping the timer, in 5200 milliseconds.
Let’s see how this interface is implemented. On top of seeing what’s behind that nice facade, this will let us get more familiar with the std::thread
interface by studying a simple example of its usage, and will also show us the drawbacks of the library.
The interface of Timer
The interface of the Timer
object is this:
This looks more like a C++20 interface, with auto
as a type in the interface. To make it compliant with C++17, we could adjust it with templates:
Even though the templates don’t add any information here. The code was more concise without them, which is a hopeful sign for C++20.
Implementation of setTimeout
Here is the implementation of setTimeout
. We will go through it line by line afterwards:
The first line sets the flag that controls if the timer is active or inactive, to set it as active:
Perhaps calling the variable active
instead of clear
would have allowed to have a positive name and made the code easier to read.
Next up we instantiate a thread object, by using its constructor that accepts a function:
That (lambda) function starts by checking if the timer is still active (otherwise it return
s immediately) as it could have been stopped by another function as we will see later. If it is active, it waits for the indicated delay
:
The sleep_for
function makes the thread it is invoked on (here, the one associated with the std::thread
we’re building) wait for at least the indicated delay. In practice it could be a little longer if the OS is not ready to hand back the execution to the thread.
Then we check again if the timer is still active, and if it is we invoke the function passed to setTimeout
:
Then we finish executing the constructor of the std::thread
:
To understand what’s happening here, we need to realize that there are two things we call “threads” here:
- the real thread that is controlled by the OS,
- the thread object, of type
std::thread
, in our program.
At the end of the construction of the thread object, the real thread starts executing the code of the above lambda (or at least as soon as the OS allows it).
But this thread object has a very short life: it will be destroyed at the end of the setTimeout
function. And we would like the real thread to outlive the thread object. To to this, we detach
one from the other:
The real thread can then live on its own life even after the thread object is destroyed at the end of setTimeout
function:
Implementation of setInterval
If the implementation of setTimeout
is clear for you, the one of setInterval
shouldn’t be a problem. Even better, a good exercise would be to try to code it up yourself.
I’m always curious to know about how many people do take the time to pause, set the blog post aside, and code up the example. If you do this, you will learn more than by a simple reading. To make it easier, here is an online compiler webpage with all the code already written except the implementation of setInterval
.
Once you’ve tried it (or if you don’t), here is the implementation in the library:
This is the same technology as the one used for setTimeout
: we create a thread object that starts by being linked to a real tread, then we .detach
it so that they have their separate lives (even if the one of the thread object is about to end smashed against a closing brace).
The lambda function of the thread repeatedly checks if the timer is still active, waits for the interval time and executes the function.
Finally, to stop the timer, the stop
method sets the clear
flag:
The drawbacks of the library
Why shouldn’t we use this library in production? What do you think? Mauser broomhandle serial number.
One issue is the very fact that it uses threads. Indeed, the JavaScript equivalent uses an event loop, and does not create a new thread for each invocation of setTimeout
or setInterval
.
Also, the clear
flag is read and written from several threads, and – correct me if I’m wrong – there is nothing to protect it from a race condition.
Another library that allows to use timers is C++ is Boost Asio, and it does use an event loop. But it’s a much, much larger library, planned to be integrated in standard C++. But that’s a topic for another post.
You will also like
Dev C 2b 2b Delay Function Example
Become a Patron!Share this post! Don't want to miss out ? Follow:
Delay in C: delay function is used to suspend execution of a program for a particular time.
Declaration: void delay(unsigned int);
Here unsigned int is the number of milliseconds (remember 1 second = 1000 milliseconds). To use delay function in your program you should include the 'dos.h' header file which is not a part of standard C library.
Delay in C program
Dev C 2b 2b Delay Function Diagram
If you don't wish to use delay function then you can use loops to produce delay in a C program.
#include<stdio.h>int main()
{
int c, d;
for(c =1; c <=32767; c++)
for(d =1; d <=32767; d++)
{}
return0;
}
We have not written any statement in the loop body. You may write some statements that doesn't affect logic of the program.
C programming code for delay
#include<stdio.h>#include<stdlib.h>
main()
{
printf('This C program will exit in 10 seconds.n');
delay(10000);
return0;
}
This C program exits in ten seconds, after the printf function is executed the program waits for 10000 milliseconds or 10 seconds and then it terminates.