Delay Function In Dev C

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.

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. Every function is discussed with the arguments it needs, its description, possible errors while using that function and a sample C graphics program with its output. Global Dev Delay C graphics. C provides us with an easy way to do so. We can use a delay function for this purpose in our code. This function is imported from the “dos.h” header file in C. We can run the code after a specific time in C using delay function. New Video from SmartArt presentations on C Graphics Try this and make your C program awesome If you are trying to make a game in C this will help you.C.

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:

Delay Function In Dev C

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.

Delay Function In Dev C++

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 returns 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.

Delay Function In Dev Circuit

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: Garmin mapsource mac.

Delay Function In Dev C

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: Zelda spirit tracks train controls patch.

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?

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

Share this post!Don't want to miss out ? Follow:

Most C programmers know the sleep() function, which pauses program execution for a given number of seconds. Seconds are a vast chunk of time, especially in a computer where things happen quickly. So a desire exists for a function that delays execution for smaller slices of time. For that duty you’ll need to code your own function, similar to the delay() function I use.
The delay() function is built upon a C library function called clock().

The clock() function returns a time value in clock ticks, which is based on the processor’s speed. The value returned is of the clock_t variable type. You can use subsequent reads of the clock() function to determine elapsed time.

The following code demonstrates elapsed time as measured by the clock() function:

Here is the sample output I see on my system:

And the clock() function returned 1072.
And the clock() function returned 1107.

Of course, to make the clock() function useful, it helps to know how many clock ticks take place a second. That information is stored in the CLOCKS_PER_SEC constant.

The above code initializes both clock_t Intel dh55pj drivers for windows 10. variables to the same value at Line 9. Then a while loop spins until the difference between the values is 1. Here’s the output:

It took 999017 ticks to wait one second.
This value should be the same as CLOCKS_PER_SEC which is 1000000.

Close enough.

Because the CLOCKS_PER_SECOND value could be different on each computer, you need to do some math to figure out how to concoct a delay() function that works on all platforms. Below you see my solution, which uses a millisecond duration as the argument to delay().

Delay Function In Dev C++

At Line 26, the delay() function calculates the pause value in milliseconds (one thousandth of a second). The clock_t variables now and then are initialized at Line 27. Then the while loop waits until the proper number of milliseconds have passed.

Delay Function In Dev C++

Feel free to use the delay() function in your code when a short pause is required.