https://zhuanlan.zhihu.com/p/59178345
g++ -std=c++20 -fcoroutines co_vs_callback.cpp
'''
include <iostream>
include <thread>
include <coroutine>
include <chrono>
include <functional>
//异步调用
using call_back = std::function<void(int)>;
void Add100ByCallback(int init, call_back f) // 异步调用
{
std::thread t(init, f {
std::this_thread::sleep_for(std::chrono::seconds(3));
f(init + 100);
});
t.detach();
}
struct Add100AWaitable
{
Add100AWaitable(int init):init_(init) {}
bool await_ready() const { return false; }
int await_resume() { return result_; }
void await_suspend(std::coroutine_handle<> handle)
{
auto f = [handle, this](int value) mutable {
result_ = value;
handle.resume();
};
Add100ByCallback(init_, f); // 调用原来的异步调用
}
int init_;
int result_;
};
struct Task
{
struct promise_type {
auto get_return_object() { return Task{}; }
auto initial_suspend() { return std::suspend_never{}; }
//auto final_suspend() { return std::suspend_never{}; } //这个改成下面,添加 noexcept
auto final_suspend() noexcept { return std::suspend_never{}; }
void unhandled_exception() { std::terminate(); }
void return_void() {}
};
};
/现在我们把通过回调函数的异步调用 Add100ByCallback 改成协程 Add100ByCoroutine -> Add100AWaitable(封装了异步调用 Add100ByCallback),
协程和普通的回调函数不同,可以直接得到add之后的结果。普通的回调函数是不可以有返回值的。协程如下:/
Task Add100ByCoroutine(int init, call_back f)
{
int ret = co_await Add100AWaitable(init);
ret = co_await Add100AWaitable(ret);
ret = co_await Add100AWaitable(ret);
f(ret);
}
int main()
{
Add100ByCallback(5, [](int value){ std::cout<<"get result: "<<value<<"\n"; });
Add100ByCoroutine(10, [](int value){ std::cout<<"get result from coroutine1: "<<value<<"\n"; });
Add100ByCoroutine(20, [](int value){ std::cout<<"get result from coroutine2: "<<value<<"\n"; });
Add100ByCoroutine(30, [](int value){ std::cout<<"get result from coroutine3: "<<value<<"\n"; });
Add100ByCoroutine(40, [](int value){ std::cout<<"get result from coroutine4: "<<value<<"\n"; });
getchar();
}
'''