lang:cpp:coroutine
Ceci est une ancienne révision du document !
co_await
#include <iostream> #include <experimental/generator> std::experimental::generator<int> loop(int iterations) { for (int i = 0; i < iterations; i++) { co_yield i; } } int main() { for (int i : loop(100)) { std::cout << i << std::endl; } }
co_return et co_await
#include <future> #include <iostream> std::future<int> t() { return std::async([]() { std::cout << "I" << std::endl; return 1; }); } std::future<void> foobar() { std::cout << " First call !\n"; std::cout << " Ret " << co_await t() << std::endl; std::cout << " Still first call !\n"; std::cout << " Ret2 " << co_await t() << std::endl; std::cout << " Only after resumed !\n"; } void tt() { auto ttt = foobar(); for (int i = 0; i < 10; i ++) std::cout << i << std::endl; for (int i = 0; i < 100; i++) std::this_thread::sleep_for(std::chrono::milliseconds(10)); ttt.wait(); } int main() { tt(); }
Retour possible :
First call ! Ret I 0 1 2 1 Still first call ! Ret2 3 4 5 6 7 8 9 I 1 Only after resumed !
lang/cpp/coroutine.1573820387.txt.gz · Dernière modification : 2019/11/15 13:19 de root