
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create timer using C++11?
The term "timer" is used to define the time based operations on hours, minutes, and seconds. Here, we can use the chrono library of C++ that helps to set the different timer operations like countdown, delayed, current time, etc. In this article, we will learn how to create a timer using C++.
Timer Properties
There are various properties to consider for building a timer in C++ program:
- Integer: int (milliseconds to wait until to run code).
- Boolean: bool (If this is true, it returns instantly, and run the code after specified time on another thread).
- The variable arguments (exactly we want to feed to std::bind).
- We can change the chrono::milliseconds to nanoseconds / microseconds etc. or to change the precision.
C++ Program to Create a Count Down Timer
To create a countdown timer for 1 minute, it uses various STL functions of C++, and each function plays a different role with the help of the chrono library. The chrono library is used to handle the operations of time and date. Below are listed functions:
- steady_clock::now(): By using now() with steady clock, we can retrieve the current time.
- duration_cast<chrono::seconds>(tend - chrono::steady_clock::now()).count(): This calculates the timer for remaining time in seconds until the countdown ends by subtracting the current time from the end time and convert the result into seconds.
- this_thread::sleep_for(chrono::seconds(1)): It pauses the execution of the program for one second, after each seconds it updates to terminal till the timer end.
Following is the basic example to create a count down timer based on above statement.
#include<iostream> #include<chrono> #include<thread> using namespace std; int main() { cout << "begin\n"; chrono::steady_clock::time_point tend = chrono::steady_clock::now() + chrono::minutes(1); while (chrono::steady_clock::now() < tend) { cout << "Time left: " << chrono::duration_cast<chrono::seconds>(tend - chrono::steady_clock::now()).count() << " seconds\n"; this_thread::sleep_for(chrono::seconds(1)); } cout << "end\n"; return 0; }
The above code obtained the following result:
begin Time left: 59 seconds Time left: 58 seconds Time left: 57 seconds .... .... .... Time left: 2 seconds Time left: 1 seconds Time left: 0 seconds end
C++ Program to Create a Delayed Timer
Here, we create a class "later" that allows delayed function execution with the help of thread and <chrono> library. It schedules functions to run either asynchronously or synchronously after a given delay. Finally, main() function passes the integer and boolean values to test the functions with different delays before ending.
Following is the illustration of delayed timer based on above statement.
#include<iostream> #include<functional> #include<chrono> #include<future> #include<cstdio> #include<thread> using namespace std; // Class that allows delayed execution of functions class later { public: // Template constructor accepting the function calls and arguments template <class callable, class... arguments> later(int after, bool async, callable&& f, arguments&&... args) { // Bind the passed function and arguments into a task function<typename result_of<callable(arguments...)>::type()> task( bind(forward<callable>(f), forward<arguments>(args)...) ); // Execute the task either asynchronously or synchronously // after the given delay if (async) { thread([after, task]() { this_thread::sleep_for(chrono::milliseconds(after)); // Execute the task asynchronously task(); // Detach the thread so it runs independently }).detach(); } else { this_thread::sleep_for(chrono::milliseconds(after)); // Execute the task synchronously task(); } } }; // simple function that does nothing void test1(void) { return; } // function that prints the passed integer value void test2(int a) { printf("result of test 2: %d\n", a); return; } int main() { later later_test1(3000, false, &test1); later later_test2(1000, false, &test2, 5); later later_test3(3000, false, &test2, 10); return 0; }
The above code produces the following result:
result of test 2: 5 result of test 2: 10