Monday, October 24, 2016

co_wait co_return co_yield


coroutine can yield from generator, return from future and wait a async lambda
#include "stdafx.h"
#include <experimental\generator>
#include <experimental\coroutine>
#include <future>
#include <iostream>
#include <string>

using namespace std;
using namespace std::experimental;


future<int> compute()
{
 int ret= co_await std::async([]
 {
  return 30;
 });

 co_return ret;
}

std::experimental::generator<int> gen()
{
 for (int i = 0; i < 10; ++i)
  co_yield i;
};

int main()
{
 for (auto v : gen())
 {
  cout << v << endl;  
 }

 auto i = compute();
 cout << i.get() << endl;
 std::string s;
 getline(cin, s);
    return 0;
}


No comments:

Post a Comment