task_condition_variable - Melnytskyi/fast_task GitHub Wiki
class task_condition_variable {
public:
void wait(std::unique_lock<mutex_unify>& lock);
bool wait_for(std::unique_lock<mutex_unify>& lock, size_t milliseconds);
bool wait_until(std::unique_lock<mutex_unify>& lock, std::chrono::high_resolution_clock::time_point time_point);
void notify_one();
void notify_all();
bool has_waiters();
void callback(std::unique_lock<mutex_unify>& mut, const std::shared_ptr<task>& task);
};
The class task_condition_variable
is a synchronization primitive that allows one or more threads to wait until a shared state is notified. The class is designed to be used with the task
class. The class is similar to the std::condition_variable
class, but there is no false wakeups and it is designed to work with the task
class, but still allows cross usage with native threads.
void wait(std::unique_lock<mutex_unify>& lock)
Blocks the current thread until the condition variable is notified. The function unblocks when the condition variable is notified, the lock is reacquired, and the function returns.
bool wait_for(std::unique_lock<mutex_unify>& lock, size_t milliseconds)
Blocks the current thread until the condition variable is notified or the specified timeout duration has elapsed. The function unblocks when the condition variable is notified, the lock is reacquired, and the function returns. If the timeout duration has elapsed, the function returns false.
bool wait_until(std::unique_lock<mutex_unify>& lock, std::chrono::high_resolution_clock::time_point time_point)
Blocks the current thread until the condition variable is notified or the specified time point is reached. The function unblocks when the condition variable is notified, the lock is reacquired, and the function returns. If the time point is reached, the function returns false.
void notify_one()
Unblocks one of the threads that are blocked on the condition variable. If no threads are blocked, the function has no effect.
void notify_all()
Unblocks all threads that are blocked on the condition variable. If no threads are blocked, the function has no effect.
bool has_waiters()
Returns true if there are threads that are blocked on the condition variable, false otherwise.
void callback(std::unique_lock<mutex_unify>& mut, const std::shared_ptr<task>& task)
Sets the callback function for the condition variable. The callback function is called when the condition variable is notified. The callback function is executed in the context of the thread that calls the
notify_one
ornotify_all
function.
task_condition_variable cv;
mutex_unify mut(/*any mutex*/);
std::shared_ptr<task> t = std::make_shared<task>([&cv, mut]() {
std::lock_guard lock(mut);
//...
cv.notify_one();
});
std::unique_lock<mutex_unify> lock(mut);
cv.wait(lock);
//...