-
Notifications
You must be signed in to change notification settings - Fork 1
/
async.cpp
59 lines (49 loc) · 1.72 KB
/
async.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* 使用无栈协程实现异步调用链
*/
#include "coroutine_utils.hpp"
#include <iostream>
#include <memory>
#include <thread>
#include <chrono>
using namespace std::chrono;
class async_work : public coroutine_base, public std::enable_shared_from_this<async_work> {
public:
async_work(std::string name) : _name(std::move(name)) {}
void operator()() {
COROUTINE_BEGIN
COROUTINE_YIELD(run());
std::cout << _name << " work 1 done. back to thread: " << std::this_thread::get_id() << std::endl;
COROUTINE_YIELD(run());
std::cout << _name << " work 2 done. back to thread: " << std::this_thread::get_id() << std::endl;
COROUTINE_YIELD(run());
std::cout << _name << " work 3 done. back to thread: " << std::this_thread::get_id() << std::endl;
COROUTINE_YIELD(run());
std::cout << _name << " work 4 done. back to thread: " << std::this_thread::get_id() << std::endl;
COROUTINE_YIELD(run());
std::cout << _name << " work 5 done. back to thread: " << std::this_thread::get_id() << std::endl;
COROUTINE_END
}
void run() {
std::thread([](auto ptr) {
std::cout << ptr->_name << " working..." << std::endl;
std::this_thread::sleep_for(1s);
(*ptr)();
}, shared_from_this()).detach();
}
private:
std::string _name;
};
int main() {
std::cout << "launch w1" << std::endl;
auto w1 = std::make_shared<async_work>("w1");
(*w1)();
std::this_thread::sleep_for(0.5s);
{
std::cout << "launch w2" << std::endl;
auto w2 = std::make_shared<async_work>("w2");
(*w2)();
}
std::cout << "waiting..." << std::endl;
std::this_thread::sleep_for(10s);
}