. Advertisement .
..3..
. Advertisement .
..4..
Unlike other programming languages like Visual Basic or JavaScript, C++ standards don’t offer any timer feature. This doesn’t mean you can’t use standard libraries and system calls to implement it. This guide will show you how to use timer in C++.
Use Timer In C++
This tutorial will show you how to implement JavaScript’s setTimeout in a simple way. If you aren’t familiar with it, this method setTimeout allows you to run a function after a predefined interval of time like a timer.
With ctime
The C Time Library provides several functions designed for getting and manipulating date and time. This includes the time()
function, which gets the current time and returns a time_t value.
The time()
function generally returns the Unix epoch time – the number of seconds since 00:00:00 UTC on 1 January 1970. We can keep invoking this function to get the current time until the duration reaches the period of time we want.
Example:
#include <iostream>
#include <ctime>
#include <unistd.h>
using namespace std;
int main()
{
int hours, minutes, seconds;
cout<<"Enter the number of hours: ";
cin>>hours;
cout<<"Enter the number of minutes: ";
cin>>minutes;
cout<<"Enter the number of seconds: ";
cin>>seconds;
int total_time = hours*60*60 + minutes*60 + seconds;
time_t current_time = time(0);
while(time(0)-current_time<total_time) {
sleep(1);
}
cout << "Time is up!\n";
return 0;
}
Output:
Enter the number of hours: 0
Enter the number of minutes: 0
Enter the number of seconds: 4
Time is up!
In this program, we ask the user to give the desired duration they want to wait before being welcomed by a message. The program evaluates this and calculates the number of seconds it needs to delay. To learn more about variable declaration, check out this guide.
The job of the while loop is to keep asking the time()
function for the current time and doing a subtraction to see whether it has reached the desired duration. When the time is up, the while loop ends, and the program prints a message to the output.
With chrono
While ctime provides simple C-style date and time functions, you will need to use the chrono library to create a timer in milliseconds. You will need C++11 to take advantage of this library.
It supports all three main types of time notions (durations, time points, and clocks) with various levels of precision. In which durations are measured in time spans like two minutes, a hundred milliseconds, or one hour. Durations are presented with the duration class in the chrono library. This class template couples a period precision and a count representation.
We also use the std::this_thread::sleep_for
function to block the current thread’s execution for a specified duration.
Example:
#include <iostream>
#include <chrono>
#include <unistd.h>
#include <thread>
typedef std::chrono::high_resolution_clock Clock;
using namespace std;
using namespace std::chrono_literals;
int main()
{
int milliseconds;
cout<<"Enter the number of milliseconds: ";
cin>>milliseconds;
std::chrono::milliseconds ms{milliseconds};
auto start = std::chrono::high_resolution_clock::now();
std::this_thread::sleep_for(ms);
auto end = std::chrono::high_resolution_clock::now();
cout << "Time is up!!\n";
return 0;
}
Output:
Enter the number of milliseconds: 1500
Time is up!!
The program asks for the number of milliseconds and converts it to the duration type with the chrono library. This duration is passed to the std::this_thread::sleep_for
function. When the time is up, the program prints a message.
Conclusion
You can use timer in C++ with the ctime and chrono libraries. While the ctime provides basic date and time operations, the chrono’s functions can deliver a higher level of accuracy.
Leave a comment