Skip to content

Conversation

@poor-circle
Copy link
Collaborator

Why

FutureAwaiter should adapt signal cancellation.

What is changing

Add support of cancellation for FutureAwaiter.

Now co_await std::move(future) could be canceled by signal.

Example

template <typename Callback>
void sum(int a, int b, Callback&& callback) const {
    std::thread([callback = std::move(callback), a, b]() mutable {
        callback(a + b);
    }).detach();
}
async_simple::executors::SimpleExecutor ex1(2);
void test() {
    auto lazy = [&]() -> Lazy<> {
        Promise<int> pr;
        auto fut = pr.getFuture();
        sum(1, 1, [pr = std::move(pr)](int val) mutable {
            std::this_thread::sleep_for(std::chrono::seconds::max());
            pr.setValue(val);
        });
        async_simple::SignalType type = None;
        try {
            co_await std::move(fut);
        } catch (const async_simple::SignalException& e) {
            type = e.value();
        } catch (...) {
        }
        EXPECT_EQ(type, async_simple::Terminate);
    };
    syncAwait(collectAll<async_simple::Terminate>(
        lazy().via(&ex1),
        async_simple::coro::sleep(std::chrono::microseconds{10}).via(&ex1)));
}

@poor-circle poor-circle force-pushed the main branch 2 times, most recently from 92a10f7 to 93a503c Compare June 11, 2025 06:46
Copy link
Collaborator

@ChuanqiXu9 ChuanqiXu9 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@poor-circle poor-circle merged commit b72767d into alibaba:main Jun 11, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants