task_query - Melnytskyi/fast_task GitHub Wiki

Declaration

    class task_query {
    public:
        task_query(size_t at_execution_max = 0);
        ~task_query();
        void add(std::shared_ptr<task>&);
        void add(std::shared_ptr<task>&&);
        void enable();
        void disable();
        bool in_query(const std::shared_ptr<task>& task);
        void set_max_at_execution(size_t val);
        size_t get_max_at_execution();
        void wait();
        bool wait_for(size_t milliseconds);
        bool wait_until(std::chrono::high_resolution_clock::time_point time_point);
    };

Description

The task_query class is designed to manage and execute tasks asynchronously, with control over the maximum number of tasks that can be executed concurrently. It provides mechanisms to add tasks to the queue, control the execution state (enabled or disabled), and wait for tasks to complete.

Functions

task_query(size_t at_execution_max = 0);

Constructor that initializes a task query with an optional maximum number of concurrently executing tasks.

~task_query();

Destructor that ensures all tasks have completed before destroying the task query object.

void add(std::shared_ptr<task>&);

Adds a task to the queue. If the task query is enabled and not at its concurrency limit, the task starts immediately.

void add(std::shared_ptr<task>&&);

Adds a task to the queue using move semantics. This overload allows adding temporary tasks to the queue.

void enable();

Enables the task query, allowing tasks to start executing if not already at the concurrency limit.

void disable();

Disables the task query, preventing new tasks from starting.

bool in_query(const std::shared_ptr<task>& task);

Checks if a given task is currently in the queue.

void set_max_at_execution(size_t val);

Sets the maximum number of tasks that can be executed concurrently.

size_t get_max_at_execution();

Returns the current maximum number of concurrently executing tasks.

void wait();

Blocks the calling thread until all tasks in the queue have completed.

bool wait_for(size_t milliseconds);

Blocks the calling thread for a specified duration or until all tasks have completed, whichever comes first. Returns true if all tasks completed in the given timeframe.

bool wait_until(std::chrono::high_resolution_clock::time_point time_point);

Blocks the calling thread until a specified time point or until all tasks have completed, whichever comes first. Returns true if all tasks completed before the time point.

Remarks

When max_at_execution is set to 0, then no one task will be executed.

set_max_at_execution to zero when the task_query is enabled will not stop the execution of the tasks that are already running. And new tasks will not be queued for execution.

This class is thread-safe, allowing multiple threads to add tasks and control the query state concurrently.

Example

#include <memory>
#include "tasks.hpp"

int main() {
    task_query tq(2); // Allow up to 2 tasks to run concurrently.

    auto task1 = std::make_shared<task>([]() {
        // Task 1 work...
    });

    auto task2 = std::make_shared<task>([]() {
        // Task 2 work...
    });

    auto task3 = std::make_shared<task>([]() {
        // Task 3 work...
    });

    tq.add(task1);
    tq.add(task2);
    tq.add(task3);

    tq.enable(); // Start executing tasks.

    // Wait for all tasks to complete.
    tq.wait();

    return 0;
}
⚠️ **GitHub.com Fallback** ⚠️