c++ - “使用无堆栈协程,只有顶层例程可以暂停”是什么意思。
问题描述
我从这里找到了该声明。起初我很惊讶,因为我相信这使得无堆栈协程几乎无用(而 C++ 协程 TS 是无堆栈的)。所以我写了一个演示(在 Visual Studio 中使用 C++ coroutine TS):
#include<experimental/coroutine>
#include<iostream>
#include<thread>
#include<mutex>
#include<future>
#include<chrono>
using namespace std;
using namespace std::chrono;
using namespace std::experimental;
class AsyncQueue {
public:
class Awaitable {
friend AsyncQueue;
AsyncQueue& mQueue;
coroutine_handle<> mCoroutineHandle;
Awaitable* mNext = nullptr;
public:
Awaitable(AsyncQueue& queue):mQueue(queue){}
bool await_ready() const noexcept {
return false;
}
bool await_suspend(coroutine_handle<> coroutineHandle) noexcept
{
mCoroutineHandle = coroutineHandle;
mQueue.enqueue(this);
return true;
}
void await_resume() noexcept {}
};
private:
mutex mMutex;
Awaitable* mHead = nullptr;
Awaitable* mTail = nullptr;
void enqueue(Awaitable* awaitable){
lock_guard<mutex> g{ mMutex };
if (mTail) {
mTail->mNext = awaitable;
mTail = awaitable;
}
else {
mTail = awaitable;
mHead = mTail;
}
}
Awaitable* dequeue() {
lock_guard<mutex> g{ mMutex };
Awaitable* result = mHead;
mHead = nullptr;
mTail = nullptr;
return result;
}
public:
Awaitable operator co_await() noexcept {
return Awaitable{ *this };
}
bool poll() {
Awaitable* awaitables = dequeue();
if (!awaitables) {
return false;
}
else {
while (awaitables) {
awaitables->mCoroutineHandle.resume();
awaitables = awaitables->mNext;
}
return true;
}
}
};
AsyncQueue toBackgroundThread;
AsyncQueue toMainThread;
std::future<void> secondLevel(int id)
{
co_await toBackgroundThread;
cout << id << " run on " << this_thread::get_id() << endl;
co_await toMainThread;
cout << id << " run on " << this_thread::get_id() << endl;
}
std::future<void> topLevel() {
co_await secondLevel(1);
co_await secondLevel(2);
}
void listen(AsyncQueue& queue) {
while (true) {
if (!queue.poll()) {
this_thread::sleep_for(100ms);
}
}
}
int main() {
thread([]() {
listen(toBackgroundThread);
}).detach();
topLevel();
listen(toMainThread);
}
协程topLevel
调用了两个secondLevel
(我认为这是可挂起的非顶级例程),并且工作正常。上面的代码打印:
1 run on 16648
1 run on 3448
2 run on 16648
2 run on 3448
从那个答案中,据称This prohibits providing suspend/resume operations in routines within a general-purpose library.
我在这里没有看到任何禁令。
解决方案
在 的每次调用中co_await
,只有顶层协程被挂起。要挂起较低级别,该级别必须显式挂起自身。到那时,它现在是当前的“顶级”。因此,在每种情况下,只有当前的顶层会被挂起。
将此与纯粹假设的堆栈式协程库进行比较:
//This function will always print the same thread ID.
void secondLevel(int id)
{
while(!toBackgroundThread.poll())
suspend_coroutine();
cout << id << " run on " << this_thread::get_id() << endl;
while(!toBackgroundThread.poll())
suspend_coroutine();
cout << id << " run on " << this_thread::get_id() << endl;
}
void topLevel() {
secondLevel(1);
secondLevel(2);
}
void listen(AsyncQueue& queue) {
while (true) {
if (!queue.poll()) {
this_thread::sleep_for(100ms);
}
}
}
int main() {
thread([]() {
listen(toBackgroundThread);
}).detach();
auto coro = create_coroutine(topLevel);
coro.switch_to();
toMainThread.ready(); //Notes that the main thread is waiting
while (true) {
if (!toMainThread.poll()) {
coro.switch_to();
}
}
};
topLevel
没有任何明确的悬挂机制。然而,只要它调用的任何函数暂停执行,它的执行就会暂停。由给定的函数及其调用的所有内容定义的整个调用堆栈create_coroutine
暂停。这就是堆栈式协程的工作方式。
这就是与无堆栈协同程序形成鲜明对比的情况。在无堆栈版本中,每个需要挂起的函数都必须经过专门编码才能这样做。因此不再是真正的“通用”;它现在是暂停场景的特殊情况。
推荐阅读
- python - Python geopandas - 编辑地理数据
- python - 我哪里错了
- cytoscape - 删除了冗余边,但未创建边表列
- amazon-web-services - AWS InvalidSignatureException,从 docker 容器运行时签名已过期
- reactjs - 重构一些代码我发现了这个。我认为它应该在构造函数中。关于这在课堂之外有什么想法吗?
- php - PHP MySQL 选择特定/非特定模式
- javascript - 成本:调用 if 与调用函数
- wolfram-mathematica - 如何找到三个量子比特密度矩阵 usign mathematica 的部分转置?
- kotlin - 执行集合操作时,是否可以修改底层集合?
- android - 无法使用 Dpad 导航到 ViewPager2 中的下一个项目以获得可访问性