CppCoroutines
CppCoroutines copied to clipboard
不然报错找不到std::exchange 我的GCC是12.1.1的
一. 在initial_suspend挂起点DispatchAwaiter 是eager start 如果在DispatchAwaiter分发到另一个线程启动会遇到问题 比如主线程创建{ Task task = CreateTask();} 又立即析构task, raii里h.destroy(), 此时task在另一个线程运行可能不处于suspend状态,就UB了。 二. 如果是这样 { Task task = CreateTask(); task.get_result();}也好像依然有问题 按照cppreference co_return按以下顺序 1. call promise.return_value(expr) 2. destroys all variables...
```cpp Generator generator = Generator::from(1, 2, 3, 4).map([](int i) { return i * 2; }); ``` 在这个例子中,由 from 生成的对象在对 generator 消费前已经析构掉了,导致后续出错 我有几个思路。 #### 1. 使用 c++23 的 deducing this. 把原map函数限定为左值引用,然后添加一个值传递的重载...
Task Producer(Channel &channel) 改成 Task Producer(Channel &channel) 去掉 co_await 10ms会core掉
``` [ 12%] Building CXX object CMakeFiles/03.functional.cpp.dir/03.functional.cpp.o In file included from /home/ubuntu/CppCoroutines/03.functional.cpp:5: /usr/include/c++/10/coroutine:295:2: error: #error "the coroutine header requires -fcoroutines" ``` 不知道为啥`cmake ..`之后调用`make`报错了。更改了一下`cmake_minimum_required`其他没改。g++是g++10 ``` cmake_minimum_required(VERSION 3.16) ``` 对cmake不是很懂希望大佬们别见怪。
Exeutor.h中的execute()函数调用的std::async()捕获了AsyncExecutor的this指针,而AsyncExecutor的提前析构导致部分资源可能提前释放了,导致程序terminate; 我想的办法: 1. std::shared_ptr延长生命周期,但是Executor创建要使用std::make_shared类似的函数,那么class TaskPromise需要显示调用该类函数创建资源Executor executor,但是promise_type类的创建没有从文章和代码中找到相关的自定义构造函数信息; 2. 在析构函数中加入std::binary_semaphore等信号量,等待所有std::futures取完后发出析构函数继续信号,但是仍然程序terminate; 3. 类似NewThreadExecutor的std::thread()啥也不管,不获取futures;反正execute()承担的最后一项工作是handle.resume(),没有完成这个工作,协程内容不会加载到函数中,那么std::async()只执行func()内容即可,程序正常起来了; 不知道我的分析对不对~ (─‿─)
06 死锁
