Skip to content

Is capy::delay thread-safe? #229

@infotraining

Description

@infotraining
    capy::thread_pool thd_pool;
    capy::Executor auto ex = thd_pool.get_executor();
    // ex.context().make_service<capy::detail::timer_service>(); // this line commented out causes ThreadSanitizer warning
    std::latch done(no_of_tasks);

    auto delay_task = [&](size_t index) -> capy::task<void> {
        co_await capy::delay(10ms * index);
    };

    for (int i = 0; i < no_of_tasks; ++i)
    {
        capy::run_async(thd_pool.get_executor(), [&]() { done.count_down(); }, [&](std::exception_ptr) { done.count_down(); })(delay_task(i));
    }

    thd_pool.join();
    done.wait();

Tests using capy::delay run on a thread_pool{1}. When the pool contains more threads, ThreadSanitizer reports a warning.
Should the timer_service- or any other service used by the execution context - be created before running any asynchronous tasks, or is it safely and lazily created on demand in await_suspend() (in this case inside delay_awaitable with ts_ = &env->executor.context().use_service<detail::timer_service>())?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions