Scheduling tasks with std::chrono

In modern C++, the <chrono> library provides a powerful way to work with time durations and time points. This makes it possible to schedule tasks or events based on specific time intervals. In this blog post, we’ll explore how to schedule tasks using std::chrono in C++.

Creating a Timepoint

Before we can schedule a task, we need to create a timepoint representing the desired start time. We can use std::chrono::system_clock::now() to get the current time and then add a duration to it to specify the desired start time.

#include <iostream>
#include <chrono>

int main() {
    // Get the current time
    auto now = std::chrono::system_clock::now();

    // Add a duration of one hour
    auto startTime = now + std::chrono::hours(1);

    // Perform some action at the specified start time
    std::cout << "Task scheduled at: " << startTime << std::endl;

    return 0;
}

In this example, we get the current time using std::chrono::system_clock::now() and store it in the variable now. We then add a duration of one hour using std::chrono::hours(1), creating a new timepoint startTime. Finally, we can perform some action at the specified start time.

Scheduling Periodic Tasks

Sometimes we need to schedule a task to run repeatedly at specific intervals. To achieve this, we can use std::chrono in combination with a loop. Here’s an example that schedules a task to run every 5 seconds:

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

int main() {
    // Get the current time
    auto startTime = std::chrono::system_clock::now();

    // Schedule the task to run every 5 seconds
    while (true) {
        // Perform the task
        std::cout << "Task performed at: " << std::chrono::system_clock::now() << std::endl;

        // Wait for 5 seconds
        std::this_thread::sleep_for(std::chrono::seconds(5));
    }

    return 0;
}

In this example, we get the current time and store it in the variable startTime. We then enter a loop where we perform the task and wait for 5 seconds using std::this_thread::sleep_for(std::chrono::seconds(5)). This ensures that the task is executed every 5 seconds.

Conclusion

The <chrono> library in C++ provides a powerful mechanism for scheduling tasks based on time durations and time points. By leveraging std::chrono, we can easily schedule and execute tasks at specific intervals, making our programs more efficient and responsive.

References: