Open In App

How to wake up a std::thread while it is sleeping?

Last Updated : 31 Mar, 2021
Like Article

In this article, we will discuss how to wake up a std::thread while it is sleeping. It is known that a thread can’t be exited when it is sleeping. So it is woken up using a command as:


Below is the pseudo-code to implement the same:


// Custom Class
struct MyClass {
    // Constructor
        : my_thread([this]() {
    // Destructor
            std::lock_guard<std::mutex> l(m_);
            stop_ = true;
    // Function that implements the
    // thread
    void thread()
        while (this->wait_for(std::chrono::minutes(2)))
    // Function to returns false when
    // the thread is stopped
    template <class Duration>
    bool wait_for(Duration duration)
        std::unique_lock<std::mutex> l(m_);
        return !c_.wait_for(l, duration, [this]() {
            return stop_;
    // Conditions Variable
    std::condition_variable c_;
    std::mutex m_;
    bool stop_ = false;
    std::thread my_thread;

Below is another example to illustrate the same:


The above command can be used as a simpler alternative to the former method. In this case, a Future is not susceptible to false wakes and doesn’t require a mutex for synchronization. Below is the pseudo-code to implement the same:


// Promise Condition
std::promise<void> pr;
// Start the thread
std::thread thr{
        [fut = pr.get_future()]{
            // Iterate until the condition
            // break
            while (true){
                if (fut.wait_for(std::chrono::minutes(2))
                    != std::future_status::timeout) return;
// When ready to stop
// Join the thread

Below is the program to illustrate the above concepts:


// C++ program to illustrate the waking
// of the thread while it is sleeping
#include <bits/stdc++.h>
using namespace std;
std::deque<int> q;
std::mutex mu;
std::condition_variable cond;
// Function to create the thread 1
void function_1()
    // Initialize a counter variable
    int count = 10;
    // Iterate until count is positive
    while (count > 0) {
        // Mutex
        std::unique_lock<mutex> locker(mu);
        // Push the current count in
        // the dequeue
        // If there is any waiting thread
        // then notify that thread
        // Decrement the count
// Function to create the thread 2
void function_2()
    // Initialize a variable to get
    // the data from the deque
    int data = 0;
    while (data != 1) {
        std::unique_lock<mutex> locker(mu);
        cond.wait(locker, []() {
            return !q.empty();
        // False Waking of thread
        data = q.back();
        // Print the message
        cout << "t2 got a value from"
                " t1"
             << data << '\n';
// Driver Code
int main()
    // Create thread 1
    std::thread t1(function_1);
    // Create thread 2
    std::thread t2(function_2);
    // Join the threads
    return 0;


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads