task - Melnytskyi/fast_task GitHub Wiki

Declaration(compacted task modifiers)

    struct task {
        void set_auto_bind_worker(bool enable = true);
        void set_worker_id(uint16_t id);

        static void schedule(std::shared_ptr<task>&& task, size_t milliseconds);
        static void schedule(const std::shared_ptr<task>& task, size_t milliseconds);
        static void schedule_until(std::shared_ptr<task>&& task, std::chrono::high_resolution_clock::time_point time_point);
        static void schedule_until(const std::shared_ptr<task>& task, std::chrono::high_resolution_clock::time_point time_point);
        static void start(std::shared_ptr<task>&& lgr_task);
        static void start(std::list<std::shared_ptr<task>>& lgr_task);
        static void start(const std::shared_ptr<task>& lgr_task);
        static bool has_result(std::shared_ptr<task>& lgr_task);
        static void await_task(const std::shared_ptr<task>& lgr_task, bool make_start = true);
        static void await_multiple(std::list<std::shared_ptr<task>>& tasks, bool pre_started = false, bool release = false);
        static void await_multiple(std::shared_ptr<task>* tasks, size_t len, bool pre_started = false, bool release = false);
        static void notify_cancel(std::shared_ptr<task>& task);
        static void notify_cancel(std::list<std::shared_ptr<task>>& tasks);
        static void callback(std::shared_ptr<task>& target, const std::shared_ptr<task>& task);
    };

Functions### Functions

void set_auto_bind_worker(bool enable = true) (setter)

Enables or disables the automatic binding of a task to a worker thread. If enabled, the task is executed by a worker thread as soon as it is scheduled. Disabling it allows manual binding of the task to a worker.

void set_worker_id(uint16_t id) (setter)

Binds the task to a specified worker thread by ID. This action disables automatic binding.

void schedule(std::shared_ptr<task> task, size_t milliseconds)

Schedules a task to run after a delay specified in milliseconds. The scheduling has no effect if the task is already scheduled or has been completed.

void schedule_until(std::shared_ptr<task> task, size_t milliseconds)

Schedules a task to run at a specified time point, measured in milliseconds. This is ineffective for tasks that are already scheduled or completed.

void start(std::shared_ptr<task> lgr_task)

Initiates a task. There is no effect if the task has already started or is completed.

void start(std::list<std::shared_ptr<task>> lgr_task)

Initiates a list of tasks. This does not affect tasks that have already begun or are finished.

bool has_result(std::shared_ptr<task> lgr_task)

Returns true if the task has finished, otherwise returns false.

void await_task(std::shared_ptr<task> lgr_task, bool make_start = true)

Waits for a task to complete. If make_start is true, the task will start if it has not already done so.

void await_multiple(std::list<std::shared_ptr<task>>& tasks, bool pre_started = false, bool release = false)

Waits for a list of tasks to complete. Tasks are started if they haven't been and pre_started is true. If release is true, tasks are released after completion.

void await_multiple(std::shared_ptr<task>* tasks, size_t len, bool pre_started = false, bool release = false)

Waits for an array of tasks to finish. Tasks are started if pre_started is true and released upon completion if release is true.

Declaration (compacted global modifiers)

    struct task {
        static uint16_t create_bind_only_executor(uint16_t fixed_count, bool allow_implicit_start);
        static void close_bind_only_executor(uint16_t id);

        static void create_executor(size_t count = 1);
        static size_t total_executors();
        static void reduce_executor(size_t count = 1);
        static void become_task_executor();

        static void await_no_tasks(bool be_executor = false);
        static void await_end_tasks(bool be_executor = false);

        static void clean_up();
        static void explicitStartTimer();
        static void shutDown();
    };

Functions

uint16_t create_bind_only_executor(uint16_t fixed_count, bool allow_implicit_start)

This function creates a worker thread exclusively for bound tasks and returns its ID. fixed_count specifies the thread count within the worker. Passing zero calculates the count based on current usage. allow_implicit_start enables automatic binding of tasks to this executor when set to true. Otherwise, the worker is restricted to bound tasks only.

void close_bind_only_executor(uint16_t id)

Terminates a worker thread designated exclusively for bound tasks. id represents the executor's identifier.

void create_executor(size_t count = 1)

Initiates a worker thread for general tasks. count determines the number of threads in the worker.

size_t total_executors()

Retrieves the total count of worker threads.

void reduce_executor(size_t count = 1)

Decreases the worker thread count. count indicates the number of threads to be reduced.

void become_task_executor()

Converts the current thread into a worker thread.

void await_no_tasks(bool be_executor = false)

Awaits the scheduling of all tasks. Setting be_executor to true transforms the current thread into a worker thread until there are no pending tasks.

void await_end_tasks(bool be_executor = false)

Awaits the completion of all tasks. With be_executor as true, the current thread acts as a worker thread until task completion.

void clean_up()

Removes all redundant resources. It clears unused memory, typically for debugging, such as detecting memory leaks. Its use in production code is generally discouraged.

void explicitStartTimer()

Activates the timer for worker threads. The timer will also start automatically with the first task that requires timing.

void shutDown()

Shuts down all worker threads.

Declaration (compacted self modifiers)

    struct task { 
        static void sleep(size_t milliseconds);
        static void sleep_until(std::chrono::high_resolution_clock::time_point time_point);
        static void yield();
        static size_t task_id();
        static void check_cancellation();
        static void self_cancel();
        static bool is_task();
    };

Functions

void sleep(size_t milliseconds)

Puts the current task to sleep for the given number of milliseconds.

void sleep_until(std::chrono::high_resolution_clock::time_point time_point)

Puts the current task to sleep until the specified time_point is reached.

void yield()

Allows the current task to yield execution.

size_t task_id()

Retrieves the ID of the current task.

void check_cancellation()

Checks if the current task has been marked for cancellation.

void self_cancel()

Initiates the cancellation of the current task.

bool is_task()

Determines if the current context is within a task, returning true, otherwise false.

Declaration (compacted constructors)

    struct task {
        task(
             std::function<void()> func,
             std::function<void(const std::exception_ptr&)> ex_handle = nullptr,
             std::chrono::high_resolution_clock::time_point timeout = std::chrono::high_resolution_clock::time_point::min()
        );
        static std::shared_ptr<task> dummy_task();
        static std::shared_ptr<task> cxx_native_bridge(bool& checker, std::condition_variable_any& cd);
    };

Functions

task(std::function<void()> func, std::function<void(const std::exception_ptr&)> ex_handle = nullptr, std::chrono::high_resolution_clock::time_point timeout = std::chrono::high_resolution_clock::time_point::min())

Creates a task with a specified function. func is the function to be executed by the task. ex_handle is the exception handler for the task. timeout is the time point when the task should be canceled.

std::shared_ptr<task> dummy_task()

Creates a dummy task.

std::shared_ptr<task> cxx_native_bridge(bool& checker, std::condition_variable_any& cd)

Constructs a task that awaits a condition variable and inspects a checker flag. checker is the flag to be inspected. cd is the condition variable to await.

Usage example

int main() {
    task::await_task(std::make_shared<task>([]() {
        std::cout << "Hello, world!" << std::endl;
    }));
    return 0;
    // Output:
    // Hello, world!
}
    task::create_executor(2);
    task::create_bind_only_executor(2, true);

    auto t1 = std::make_shared<task>([]() {
        std::cout << "Task 1 finished" << std::endl;
    });

    auto t2 = std::make_shared<task>([]() {
        std::cout << "Task 2 finished" << std::endl;
    });

    auto t3 = std::make_shared<task>([]() {
        task::schedule(t1, 1000);
        std::cout << "Task 3 finished" << std::endl;
    });

    task::callback(t1, t2);
    task::schedule(t3, 500);

    std::list<std::shared_ptr<task>> tasks = {t1, t2, t3};
    task::await_multiple(tasks, true, true);
    std::cout << "All tasks finished" << std::endl;

    task::shutDown();
    task::clean_up();

    // Output:
    // Task 3 finished
    // Task 1 finished
    // Task 2 finished
    // All tasks finished
⚠️ **GitHub.com Fallback** ⚠️