co_wait will setup a state machine to return, future has to call get() to return
#include "stdafx.h"
#include <experimental\coroutine>
#include <iostream>
#include <string>
#include <future>
#include <thread>
using namespace std;
using namespace std::experimental;
std::future<int> produceAsync()
{
return std::async(std::launch::async, [] {
cout << " future thread id:"<< std::this_thread::get_id()<<endl;
std::this_thread::sleep_for(5s);
return 42;
});
}
std::future<void> ConsumeAsync()
{
auto r = co_await produceAsync(); // set up resume state machine
cout << r << endl;
}
int main()
{
cout << "main thread id:" << std::this_thread::get_id() <<endl;
ConsumeAsync();
auto r = produceAsync();
//cout << r.get() << endl; //get causes return, no SM setup
std::string s;
getline(cin, s);
return 0;
}
Tuesday, October 25, 2016
co_await vs. future.get()
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment