In this blog , we are going to learn about How to make a thread to sleep in c++11, c++14,c++17.  The modern c++ like in c++11, c++14 or c++17 provides two interfaces to make a thread sleep for a particular amount of time.

std::this_thread::sleep_for
std::this_thread::sleep_untill

How to make a thread to sleep in c++11 using  std::this_thread::sleep_for

Syntax:

template <class Rep, class Period>void sleep_for (const chrono::duration<Rep,Period>& duration_time);

This function takes a duration time as an argument and make the calling thread to sleep for that particular  time duration. Let see an example.

#include<iostream>
#include <thread>
#include <chrono>

void foo_thread_func()
{
        int i = 0;
        while (i < 5)
        {
                i++;
                std::cout << "hello"<< i<< std::endl;
                // Sleep this thread for 100 MilliSeconds
                std::this_thread::sleep_for(std::chrono::milliseconds(2000));
                std::cout << "world"<< std::endl;
        }
}

int main()
{
        std::thread mythread(&foo_thread_func);
        mythread.join(); // join main
        return 0;
}

compile using

g++ -std=c++11 main.cpp -lpthread

run ./a.out

hello1
world
hello2
world
hello3
world
hello4
world
hello5
world

Explanation:

  1.  The thread executes function foo_thread_func , and as soon execution reach inside while it prints hello and i after that thread sleeps for 2000 milliseconds.
  2. After 2000 milliseconds it prints “world”
  3. This will continue until while ends.

We can use following std::chrono identifier to make thread sleep for different amount of time from nanoseconds , microseconds , milliseconds, minutes or hours.

std::chrono::nanoseconds
std::chrono::microseconds
std::chrono::milliseconds
std::chrono::seconds
std::chrono::minutes
std::chrono::hours

Sleep Until a Time point std::this_thread::sleep_untill

template< class Clock, class Duration >void sleep_until( const std::chrono::time_point<Clock,Duration>& sleep_time );

Blocks the execution of the current thread until specified sleep_time has been reached. Let us see the simple example uses of std::this_thread::sleep_until

#include <iostream>
#include <thread>
#include <chrono>

// utility to print current time
// Print Current Time
void print_current_time(std::chrono::system_clock::time_point timePoint)
{
        std::time_t timeStamp = std::chrono::system_clock::to_time_t(timePoint);
        std::cout << std::ctime(&timeStamp) << std::endl;

}

void foo()
{

        std::cout<<"Time :: ";
        print_current_time(std::chrono::system_clock::now());

        // create future time point 
        std::chrono::system_clock::time_point timePoint_sleep =
                        std::chrono::system_clock::now() + std::chrono::seconds(4);

        std::cout << "Going to Sleep Until time point ";
        print_current_time(std::chrono::system_clock::now());


        // Sleep Till specified time point
        std::this_thread::sleep_until(timePoint_sleep);

        std::cout<<"Current Time :: ";
        print_current_time(std::chrono::system_clock::now());
}

int main()
{
        std::thread mythread(&foo);
        mythread.join();
        return 0;
}

Compile using

g++ -std=c++11 main.cpp -lpthread

Run

./a.out

Output:

Time :: Thu Oct  4 09:51:40 2018

Going to Sleep Until time point Thu Oct  4 09:51:40 2018

Current Time :: Thu Oct  4 09:51:44 2018

 

So , now we have knowledge of these two c++11 interface

std::this_thread::sleep_for 
std::this_thread::sleep_untill

Let me ask a question , in which case will you prefer to use sleep_for over sleep_until or vice versa? please follow this link discussion regarding this https://stackoverflow.com/questions/11200763/when-is-it-appropriate-to-use-a-c11-until-timeout-function-instead-of-the-co

 

Note: I found a very good link which has some important points regrading thread creation and mistakes  (http://www.acodersjourney.com/2017/08/top-20-cplusplus-multithreading-mistakes/)

Ref:

https://en.cppreference.com/w/cpp/chrono/time_point

c++ 11 thread tutorial



Related Contents to follow