task_limiter - Melnytskyi/fast_task GitHub Wiki

Declaration

    
    class task_limiter {
    public:
        void set_max_threshold(size_t val);
        void lock();
        bool try_lock();
        bool try_lock_for(size_t milliseconds);
        bool try_lock_until(std::chrono::high_resolution_clock::time_point time_point);
        void unlock();
        bool is_locked();
    };

Description

The task_limiter class is a synchronization primitive that can be used to limit the number of tasks that can run concurrently. This class also checks for deadlocks and throws an exception if a deadlock is detected.

Functions

set_max_threshold(size_t val)

Sets the maximum number of tasks that can run concurrently to val.

lock()

Blocks the calling thread until the task_limiter can be locked.

try_lock()

Tries to lock the task_limiter. Returns true if the lock was successful, false otherwise.

try_lock_for(size_t milliseconds)

Tries to lock the task_limiter for the specified duration. Returns true if the lock was successful, false otherwise.

try_lock_until(std::chrono::high_resolution_clock::time_point time_point)

Tries to lock the task_limiter until the specified time point. Returns true if the lock was successful, false otherwise.

unlock()

Unlocks the task_limiter.

is_locked()

Returns true if the task_limiter is locked, false otherwise.

Example

    task_limiter limiter;
    limiter.set_max_threshold(2);
    limiter.lock();
    //...
    limiter.unlock();