我从here找到了该语句。起初我很惊讶,因为我相信使无栈协程几乎毫无用处(而C ++协程TS是无栈的)。所以我写了一个演示(在Visual Studio中使用C ++协程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.
在这里没有任何禁忌。
答案 0 :(得分:0)
在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
提供的函数定义的整个调用堆栈及其调用的所有内容都将挂起。这就是大量协程的工作方式。
这是与无堆栈协程相比的对比。在无堆栈版本中,必须对每个需要挂起的函数进行专门编码来这样做。因此,这不再是真正的“通用目的”;现在对挂起方案有特殊情况。