In C, we have been using std::time( ) for getting data and time . However, in C++ 11, there is concept of std::chrono( ) which give flexibility of getting date and time of the system. The std::chrono namespace give us several utilities for getting time interval, system time, date, etc. The std::chrono library is much more user friendly and flexible library for dealing with time related operations.
Header File
#include <chrono>
using namespace std::chrono;
sleep_for( ) function
The function sleep_for( ), as it is clear from name is used for sleeping or blocking current thread for specified duration. We can specify the duration for which we want to block the current thread as an parameter to sleep_for( ). For understanding threads please go through below link.
C++ Multithreading: Understanding Threads
Let us see below simple example, where we are blocking main thread for 5 secs using sleep_for( ) utility.
#include <iostream> // main Header
#include <thread> // for this_thread
#include <chrono> // for std::chrono::seconds
using namespace std; // for namespace
using namespace chrono;
int main()
{
cout<<"Enter main"<<endl;
std::this_thread::sleep_for (std::chrono::seconds(5));
cout<<"Main exited after 5 secs"<<endl;
return 0;
}
Output
Let us see one more example where we are blocking one thread for 500 milliseconds sec but let other thread to continue its job of printingData.
#include<thread> //for threads
#include<iostream> //main header
#include<chrono> // for chrono
using namespace std;//for namespace
void printingTask(int value)
{
for(int i =0;i<10;i++)
{
cout<<"printing value"
<<i << endl;
}
}
void sleepingTask()
{
cout<<"sleeping Task Enter"<<endl;
std::this_thread::sleep_for(
std::chrono::milliseconds(500)
);
cout<<"sleeping Task Exit"<<endl;
}
int main()
{
std::thread job1(sleepingTask);
std::thread job2(printingTask,2);
job1.join();
job2.join();
cout<<"Main job done"<<endl;
}
As demonstrated above with sleep_for( ), only current thread will block but other thread will continue its job as it is.
Date and Time
In order to get the current Date and Time of the system, C++ 11 std::chrono support 3 types of clock.
a) system_clock
This will give wall clock time of the system . Need to call system_clock::now( ) which will give us system current real time_point.
Let us see below simple example of getting current time of the system using system_clock.
#include <iostream> //main header
#include <ctime> // converting time into readable format
#include <chrono> // for system_clock
using std::chrono::system_clock;
int main ()
{
system_clock::time_point clockDateAndTime
= system_clock::now();
// chrono lib util to get
// current date and time
std::time_t t1 =
system_clock::to_time_t ( clockDateAndTime );
// to_time_t will convert data
// into time_t format
std::cout << "Today Date and Time " << ctime(&t1);
return 0;
}
In above example time_point represents time with respect to system_clock.
b) steady_clock
Mainly used for getting time interval . It is monotonic clock means now will never returns a value lower than previous call.
Let us see below example, where we have called steady_clock::now( ) 2 times. This will help to find out how long the main thread slept between the 2 steady_clock::now( ) calls.
// steady_clock example
#include <iostream> //main header
#include <thread> //for threads
#include <chrono> //for chrono
using namespace std::chrono;
int main ()
{
steady_clock::time_point t1 = steady_clock::now();
std::this_thread::sleep_for (std::chrono::seconds(1));
steady_clock::time_point t2 = steady_clock::now();
duration<double> timeThreadSleep =
duration_cast<duration<double>>(t2 - t1);
std::cout << "Sleep for "
<< timeThreadSleep.count()
<< " seconds.";
std::cout << std::endl;
return 0;
}
Output
c) high_resolution_clock
As it is clear from name high_resoltion_clock are clock with high resoultion means higher precision.They are also having shortest tick. It is also work as alias for system_clock and steady_clock .
Lets first see basic example for high_resolution_clock doing both job of steady_clock and system_clock.
#include <iostream> // main header
#include <ctime> // for time
#include <chrono> // for chrono
#include<thread> // for threads
using namespace std::chrono;
int main ()
{
high_resolution_clock::time_point t1 =
high_resolution_clock::now();
std::time_t t21 =
high_resolution_clock::to_time_t ( t1 );
std::cout << "Today Date and Time " << ctime(&t21);
std::this_thread::sleep_for (std::chrono::seconds(1));
high_resolution_clock::time_point t2 =
high_resolution_clock::now();
duration<double> time_span =
duration_cast<duration<double>>(t2 - t1);
std::cout<<"time span"
<<time_span.count() << std::endl;
return 0;
}
Output
sleep_untill( ) function
sleep_untill( ) chrono utility give the flexibility of blocking or sleeping current thread for that time point . Means suppose we want to block our thread and want it to wake_up at midnight 12 clock so sleep_unitll( ) is the best option for such requirement.
Below programme an application is depicted whose job is to get current time and sleep_untill for next 10 seconds and wake up say ” hi” and again sleep_untill for next minute.
#include <iostream> // main header
#include <thread> // for std::this_thread
#include <chrono> // for std::chrono::system_clock
#include <ctime> // for std::time_t
using namespace std::chrono;
int main()
{
system_clock::time_point clockDateAndTime =
system_clock::now();
// chrono lib util to get
// current date and time
std::time_t t1 =
system_clock::to_time_t ( clockDateAndTime );
std::cout << "Today Date and Time " << ctime(&t1);
struct std::tm * localTime = std::localtime(&t1);
std::cout << "Waiting for the next minute to begin...\n";
localTime->tm_sec = localTime->tm_sec + 10;
std::this_thread::sleep_until (system_clock::from_time_t
(mktime(localTime )));
clockDateAndTime = system_clock::now();
t1 = system_clock::to_time_t ( clockDateAndTime );
std::cout << "Thread sleep for "
<< ctime(&t1);
return 0;
}
Output