Boost logo

Boost Users :

From: Keith Bennett (kanezor_at_[hidden])
Date: 2023-12-06 01:43:00


I'm using asio's awaitable. Couroutines are pretty neat.

One of the things that keeps cropping up in the ability to notify another
function that something has finished. In the past, I would use
condition_variable and wait on that.

Is there a better way to do that with coroutines? eg, I want to do
something like:

asio::awaitable<void>
function(std::mutex& mtx, condition_variable& cv)
{
    std::unique_lock lock{mtx};

    // suspend this function until notified or 30 seconds expired
    auto expired = co_await cv.wait_until(lock, std::chrono::seconds(30));
    if (expired)
    {
        // or something
        log("expired");
        co_return;
    }

    do_some_work();
}

then somewhere else I might do:

asio::awaitable<void>
somewhere_else(condition_variable& cv)
{
    do_other_work();
    cv.notify_one();
}

I understand that it's a bad idea to hold a lock during co_await. What
other problems prevent something like this from being created? Does
something like this already exist? It's not clear from asio reference. I'm
using boost 1.81.0 but I can upgrade if that is a problem

-- 
Keith Bennett


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net