📜 ⬆️ ⬇️

Resumable functions

An interesting event happened in the C ++ world last week. Microsoft announced the release of an update to the C ++ compiler in Visual Studio 2013. In itself, updating the compiler separately from Visual Studio or its service pack is already a non-trivial event for Microsoft. But even more interesting is what went into this update. The full list can be read on the link above, and I will focus on only one point - the resumable functions. For a complete understanding of the situation: Microsoft has pretty much prorolled the C ++ Standardization Committee and the gcc \ clang developers, releasing (here we must carefully) implementing the experimental and not yet approved features of the future C ++ 17 standard, based on the experimental and not yet approved features of the future C ++ 14 standard, which in turn are corrections of the C ++ 11 features that are not yet heavily incorporated into the everyday programming.

Enough geeky move, is not it?

And below will be a translation of an article from meetingcpp.com , telling about what this feature is and how to use it.

')
At the recent BUILD conference, Herb Sutter talked about the future of C ++. His report was full of beautiful examples in C ++ 11 and C ++ 14. And suddenly, straight from nowhere - resumable features. Herb is one of the authors of a document describing std :: future and resumable functions, so their very mention was not a surprise for me, but what surprised me was how much attention he paid to this topic and the fact that resumable functions will be included in the update to Visual Studio 2013 (though not in the release of VS2013, but still much earlier than the next version of the IDE).

I will start with a small spoiler: this is at least C ++ 1y, it will not be included in C ++ 14, but in the future exactly asynchronous and parallel programming will be in the trend of language development, so resumable functions will become an integral part of the new standard. In the future, this feature will be supported by all compilers, and at the moment Microsoft is walking ahead of the rest of the world with its own implementation. It's no secret that this functionality has some analogy with async / await from the C # language.

What is the resumable function?

This, in fact, is the main question that we are trying to find out. Before I begin to explain what this could be and how the N3650 document defines them , I have to make a small stop and tell what futures are, since the resumable functions are based on the assumption that std :: future will be extended with the .then () method as it is supposed in document N3634 . future is the result of an asynchronous operation. This is one of the basic concepts of asynchronous programming. future is the place where information is stored about the status of the asynchronous task and its result, if it is already available. You can call the get () method, which will wait for the asynchronous operation to complete and return its result to you (this is already implemented in the standard), or register a handler for its completion through the .then () method (which is not yet standard). The absence of .then () in C ++ 11 is one of the most criticized errors, it will probably be fixed in C ++ 14, along with some other improvements to std :: future.

C ++ 11 was added to C ++ lambdas, so that in combination it makes it possible to build a chain of asynchronous calls to lambda functions (callbacks). Now it will be possible to start the execution of the asynchronous task and respond to its completion in the handler registered via the .then () method. “Read the server’s response, then parse it, then process it, ...”. With error checking and logging along the way. This approach is commonplace in some languages, but not yet in C ++. A proper understanding of such a powerful mechanism can seriously affect how you will write code in the future.

A short example to demonstrate std :: future:

std::future<int> f_int = make_dummy_future(42); int i = f_int.get() //     f_int.then([](std::future<int> i){/* deal with it */}) //   


The idea behind the resumable function is to allow the compiler to take care of building a chain of futures attached to each other and call them correctly using .then ().
This is achieved through the announcement of two new keywords: async and await. Please note that this has nothing to do with the library std :: async, this is NOT a library, this is an extension of a programming language. The function is marked with the async keyword after its declaration, but prior to its specification on the generated exceptions:

 void resumable_function(int i) async 


So now the compiler knows that this is a resumable function. And the fun begins. Although this is a function, it is still quite limited in capabilities. The first of these is its return type - it can be either void or std :: future / std :: shared_future. Perhaps the types that can be converted to std:: (shared_) future will also be allowed, but implicit conversions are not the best solution here, so maybe the committee decides to choose strict type matching. The current document still permits to return T, implicitly converting it to
 std::future. 

resumable . await "" future, . await (, , "!").

. await - std::future, . , Hartmut Kaiser - :

std::future<uint64_t> fibonacci(uint64_t n) async { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs = std::async(&fibonacci, n-1); std::future<uint64_t> rhs = fibonacci(n-2); return await lhs + await rhs; }


resumable . lhs std::future , await, std::future .

, resumable - , await future . , , await. , await. , , .


resumable , " ?". - . , . resumable . Thomas Heller ( ) resumable .

std::future< uint64_t> fibonacci(uint64_t n) { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs_future = std::async(&fibonacci, n-1); //.unwrap(); std::future<uint64_t> rhs_future = fibonacci(n-2); return dataflow( unwrapped([](uint64_t lhs, uint64_t rhs) { return lhs + rhs; }) , lhs_future, rhs_future ); }

. , "dataflow" await . future, . ++11 ++14 - .

- resumable ?
, , . Hartmut Kaiser , resumable . , \ . , .

resumable
- ( ) resumable . - , , . async/await, (if/else, for ..). . N3650 , std::future:

future<int> f(shared_ptr str) { shared_ptr<vector> buf = ...; return str->read(512, buf) .then([](future<int> op)// lambda 1 { return op.get() + 11; }); } future<void> g() { shared_ptr s = ...; return f(s).then([s](future<int> op) // lambda 2 { s->close(); }); }

resumable :

future<void> f(stream str) async { shared_ptr<vector> buf = ...; int count = await str.read(512, buf); return count + 11; } future g() async { stream s = ...; int pls11 = await f(s); s.close(); }

resumable (, , ). . , Herb Sutter BUILD:

std::string read( std::string file, std::string suffix ) { std::istream fi = open(file).get(); std::string ret, chunk; while( (chunk = fi.read().get()).size() ) ret += chunk + suffix; return ret; }

" " - future::get() std::future. , get() then(). , .

task<std::string> read( std::string file, std::string suffix ) { return open(file) .then([=](std::istream fi) { auto ret = std::make_shared<std::string>(); auto next = std::make_shared<std::function<task()>>( [=]{ fi.read() .then([=](std::string chunk) { if( chunk.size() ) { *ret += chunk + suffix; return (*next)(); } return *ret; }); }); return (*next)(); }); }

.then() . async/await:

task<std::string> read( std::string file, std::string suffix ) __async { std::istream fi = __await open(file); std::string ret, chunk; while( (chunk = __await fi.read()).size() ) ret += chunk + suffix; return ret; }

task<std::string> , . await , .then(). __async __await , Visual Studio.

. , - . std::future, auto .then , - , . resumable . , , , async/await. resumable , "" , std::future, 1:0 resumable .

. , resumable . , ++? . . . - 2 : . - ? . , . . ? ? futures (- ). , , , . resumable (, ) " ": , , , - " ".

, resumable . std::(shared_)future void. , boost::future hpx::future. , "", . - resumable VArgs, -. , variadic templates. , , resumable , , std::future - copy/move .


, ++14. -, ( ?) ++14 ( ) - . - ++11 . , - ++y1. resumable - (TS), - WG21. , resumable , . , . , CTP Visual Studio ( . : !). __async __await.

, . ++14 .then() future, await std::future::get.


. - , - . resumable . , Cilk ( - ). - resumable - - , .

( __async\__await ?). resumable STL Boost - .

, resumable - - (. ). , , . -, ? , , , - .

, , resumable . - resumable lambda functions ? - WG21, 2013 .

- N3722
resumable . , async resumable. , resumable "resumable", - . await .

, std::future . , s, :

get(), then(), callable object s, s& const s. get().

is_ready(), future


, s::promise_type, resumable . set_value(T) set_exception(exception_ptr). s::promise_type s.


, " " "". STL , , , - . - sequence , , , ( ).

yield:

sequence<int> range(int low, int high) resumable { for(int i = low; i <= high; ++i) { yield i; } }


yield i sequence. sequence , yield. - , , . yield await .
std::future.

resumable . await "" future, . await (, , "!").

. await - std::future, . , Hartmut Kaiser - :

std::future<uint64_t> fibonacci(uint64_t n) async { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs = std::async(&fibonacci, n-1); std::future<uint64_t> rhs = fibonacci(n-2); return await lhs + await rhs; }


resumable . lhs std::future , await, std::future .

, resumable - , await future . , , await. , await. , , .


resumable , " ?". - . , . resumable . Thomas Heller ( ) resumable .

std::future< uint64_t> fibonacci(uint64_t n) { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs_future = std::async(&fibonacci, n-1); //.unwrap(); std::future<uint64_t> rhs_future = fibonacci(n-2); return dataflow( unwrapped([](uint64_t lhs, uint64_t rhs) { return lhs + rhs; }) , lhs_future, rhs_future ); }

. , "dataflow" await . future, . ++11 ++14 - .

- resumable ?
, , . Hartmut Kaiser , resumable . , \ . , .

resumable
- ( ) resumable . - , , . async/await, (if/else, for ..). . N3650 , std::future:

future<int> f(shared_ptr str) { shared_ptr<vector> buf = ...; return str->read(512, buf) .then([](future<int> op)// lambda 1 { return op.get() + 11; }); } future<void> g() { shared_ptr s = ...; return f(s).then([s](future<int> op) // lambda 2 { s->close(); }); }

resumable :

future<void> f(stream str) async { shared_ptr<vector> buf = ...; int count = await str.read(512, buf); return count + 11; } future g() async { stream s = ...; int pls11 = await f(s); s.close(); }

resumable (, , ). . , Herb Sutter BUILD:

std::string read( std::string file, std::string suffix ) { std::istream fi = open(file).get(); std::string ret, chunk; while( (chunk = fi.read().get()).size() ) ret += chunk + suffix; return ret; }

" " - future::get() std::future. , get() then(). , .

task<std::string> read( std::string file, std::string suffix ) { return open(file) .then([=](std::istream fi) { auto ret = std::make_shared<std::string>(); auto next = std::make_shared<std::function<task()>>( [=]{ fi.read() .then([=](std::string chunk) { if( chunk.size() ) { *ret += chunk + suffix; return (*next)(); } return *ret; }); }); return (*next)(); }); }

.then() . async/await:

task<std::string> read( std::string file, std::string suffix ) __async { std::istream fi = __await open(file); std::string ret, chunk; while( (chunk = __await fi.read()).size() ) ret += chunk + suffix; return ret; }

task<std::string> , . await , .then(). __async __await , Visual Studio.

. , - . std::future, auto .then , - , . resumable . , , , async/await. resumable , "" , std::future, 1:0 resumable .

. , resumable . , ++? . . . - 2 : . - ? . , . . ? ? futures (- ). , , , . resumable (, ) " ": , , , - " ".

, resumable . std::(shared_)future void. , boost::future hpx::future. , "", . - resumable VArgs, -. , variadic templates. , , resumable , , std::future - copy/move .


, ++14. -, ( ?) ++14 ( ) - . - ++11 . , - ++y1. resumable - (TS), - WG21. , resumable , . , . , CTP Visual Studio ( . : !). __async __await.

, . ++14 .then() future, await std::future::get.


. - , - . resumable . , Cilk ( - ). - resumable - - , .

( __async\__await ?). resumable STL Boost - .

, resumable - - (. ). , , . -, ? , , , - .

, , resumable . - resumable lambda functions ? - WG21, 2013 .

- N3722
resumable . , async resumable. , resumable "resumable", - . await .

, std::future . , s, :

get(), then(), callable object s, s& const s. get().

is_ready(), future


, s::promise_type, resumable . set_value(T) set_exception(exception_ptr). s::promise_type s.


, " " "". STL , , , - . - sequence , , , ( ).

yield:

sequence<int> range(int low, int high) resumable { for(int i = low; i <= high; ++i) { yield i; } }


yield i sequence. sequence , yield. - , , . yield await .
std::future.

resumable . await "" future, . await (, , "!").

. await - std::future, . , Hartmut Kaiser - :

std::future<uint64_t> fibonacci(uint64_t n) async { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs = std::async(&fibonacci, n-1); std::future<uint64_t> rhs = fibonacci(n-2); return await lhs + await rhs; }


resumable . lhs std::future , await, std::future .

, resumable - , await future . , , await. , await. , , .


resumable , " ?". - . , . resumable . Thomas Heller ( ) resumable .

std::future< uint64_t> fibonacci(uint64_t n) { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs_future = std::async(&fibonacci, n-1); //.unwrap(); std::future<uint64_t> rhs_future = fibonacci(n-2); return dataflow( unwrapped([](uint64_t lhs, uint64_t rhs) { return lhs + rhs; }) , lhs_future, rhs_future ); }

. , "dataflow" await . future, . ++11 ++14 - .

- resumable ?
, , . Hartmut Kaiser , resumable . , \ . , .

resumable
- ( ) resumable . - , , . async/await, (if/else, for ..). . N3650 , std::future:

future<int> f(shared_ptr str) { shared_ptr<vector> buf = ...; return str->read(512, buf) .then([](future<int> op)// lambda 1 { return op.get() + 11; }); } future<void> g() { shared_ptr s = ...; return f(s).then([s](future<int> op) // lambda 2 { s->close(); }); }

resumable :

future<void> f(stream str) async { shared_ptr<vector> buf = ...; int count = await str.read(512, buf); return count + 11; } future g() async { stream s = ...; int pls11 = await f(s); s.close(); }

resumable (, , ). . , Herb Sutter BUILD:

std::string read( std::string file, std::string suffix ) { std::istream fi = open(file).get(); std::string ret, chunk; while( (chunk = fi.read().get()).size() ) ret += chunk + suffix; return ret; }

" " - future::get() std::future. , get() then(). , .

task<std::string> read( std::string file, std::string suffix ) { return open(file) .then([=](std::istream fi) { auto ret = std::make_shared<std::string>(); auto next = std::make_shared<std::function<task()>>( [=]{ fi.read() .then([=](std::string chunk) { if( chunk.size() ) { *ret += chunk + suffix; return (*next)(); } return *ret; }); }); return (*next)(); }); }

.then() . async/await:

task<std::string> read( std::string file, std::string suffix ) __async { std::istream fi = __await open(file); std::string ret, chunk; while( (chunk = __await fi.read()).size() ) ret += chunk + suffix; return ret; }

task<std::string> , . await , .then(). __async __await , Visual Studio.

. , - . std::future, auto .then , - , . resumable . , , , async/await. resumable , "" , std::future, 1:0 resumable .

. , resumable . , ++? . . . - 2 : . - ? . , . . ? ? futures (- ). , , , . resumable (, ) " ": , , , - " ".

, resumable . std::(shared_)future void. , boost::future hpx::future. , "", . - resumable VArgs, -. , variadic templates. , , resumable , , std::future - copy/move .


, ++14. -, ( ?) ++14 ( ) - . - ++11 . , - ++y1. resumable - (TS), - WG21. , resumable , . , . , CTP Visual Studio ( . : !). __async __await.

, . ++14 .then() future, await std::future::get.


. - , - . resumable . , Cilk ( - ). - resumable - - , .

( __async\__await ?). resumable STL Boost - .

, resumable - - (. ). , , . -, ? , , , - .

, , resumable . - resumable lambda functions ? - WG21, 2013 .

- N3722
resumable . , async resumable. , resumable "resumable", - . await .

, std::future . , s, :

get(), then(), callable object s, s& const s. get().

is_ready(), future


, s::promise_type, resumable . set_value(T) set_exception(exception_ptr). s::promise_type s.


, " " "". STL , , , - . - sequence , , , ( ).

yield:

sequence<int> range(int low, int high) resumable { for(int i = low; i <= high; ++i) { yield i; } }


yield i sequence. sequence , yield. - , , . yield await .
std::future.

resumable . await "" future, . await (, , "!").

. await - std::future, . , Hartmut Kaiser - :

std::future<uint64_t> fibonacci(uint64_t n) async { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs = std::async(&fibonacci, n-1); std::future<uint64_t> rhs = fibonacci(n-2); return await lhs + await rhs; }


resumable . lhs std::future , await, std::future .

, resumable - , await future . , , await. , await. , , .


resumable , " ?". - . , . resumable . Thomas Heller ( ) resumable .

std::future< uint64_t> fibonacci(uint64_t n) { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs_future = std::async(&fibonacci, n-1); //.unwrap(); std::future<uint64_t> rhs_future = fibonacci(n-2); return dataflow( unwrapped([](uint64_t lhs, uint64_t rhs) { return lhs + rhs; }) , lhs_future, rhs_future ); }

. , "dataflow" await . future, . ++11 ++14 - .

- resumable ?
, , . Hartmut Kaiser , resumable . , \ . , .

resumable
- ( ) resumable . - , , . async/await, (if/else, for ..). . N3650 , std::future:

future<int> f(shared_ptr str) { shared_ptr<vector> buf = ...; return str->read(512, buf) .then([](future<int> op)// lambda 1 { return op.get() + 11; }); } future<void> g() { shared_ptr s = ...; return f(s).then([s](future<int> op) // lambda 2 { s->close(); }); }

resumable :

future<void> f(stream str) async { shared_ptr<vector> buf = ...; int count = await str.read(512, buf); return count + 11; } future g() async { stream s = ...; int pls11 = await f(s); s.close(); }

resumable (, , ). . , Herb Sutter BUILD:

std::string read( std::string file, std::string suffix ) { std::istream fi = open(file).get(); std::string ret, chunk; while( (chunk = fi.read().get()).size() ) ret += chunk + suffix; return ret; }

" " - future::get() std::future. , get() then(). , .

task<std::string> read( std::string file, std::string suffix ) { return open(file) .then([=](std::istream fi) { auto ret = std::make_shared<std::string>(); auto next = std::make_shared<std::function<task()>>( [=]{ fi.read() .then([=](std::string chunk) { if( chunk.size() ) { *ret += chunk + suffix; return (*next)(); } return *ret; }); }); return (*next)(); }); }

.then() . async/await:

task<std::string> read( std::string file, std::string suffix ) __async { std::istream fi = __await open(file); std::string ret, chunk; while( (chunk = __await fi.read()).size() ) ret += chunk + suffix; return ret; }

task<std::string> , . await , .then(). __async __await , Visual Studio.

. , - . std::future, auto .then , - , . resumable . , , , async/await. resumable , "" , std::future, 1:0 resumable .

. , resumable . , ++? . . . - 2 : . - ? . , . . ? ? futures (- ). , , , . resumable (, ) " ": , , , - " ".

, resumable . std::(shared_)future void. , boost::future hpx::future. , "", . - resumable VArgs, -. , variadic templates. , , resumable , , std::future - copy/move .


, ++14. -, ( ?) ++14 ( ) - . - ++11 . , - ++y1. resumable - (TS), - WG21. , resumable , . , . , CTP Visual Studio ( . : !). __async __await.

, . ++14 .then() future, await std::future::get.


. - , - . resumable . , Cilk ( - ). - resumable - - , .

( __async\__await ?). resumable STL Boost - .

, resumable - - (. ). , , . -, ? , , , - .

, , resumable . - resumable lambda functions ? - WG21, 2013 .

- N3722
resumable . , async resumable. , resumable "resumable", - . await .

, std::future . , s, :

get(), then(), callable object s, s& const s. get().

is_ready(), future


, s::promise_type, resumable . set_value(T) set_exception(exception_ptr). s::promise_type s.


, " " "". STL , , , - . - sequence , , , ( ).

yield:

sequence<int> range(int low, int high) resumable { for(int i = low; i <= high; ++i) { yield i; } }


yield i sequence. sequence , yield. - , , . yield await .
 std::future. 

resumable . await "" future, . await (, , "!").

. await - std::future, . , Hartmut Kaiser - :

std::future<uint64_t> fibonacci(uint64_t n) async { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs = std::async(&fibonacci, n-1); std::future<uint64_t> rhs = fibonacci(n-2); return await lhs + await rhs; }


resumable . lhs std::future , await, std::future .

, resumable - , await future . , , await. , await. , , .


resumable , " ?". - . , . resumable . Thomas Heller ( ) resumable .

std::future< uint64_t> fibonacci(uint64_t n) { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs_future = std::async(&fibonacci, n-1); //.unwrap(); std::future<uint64_t> rhs_future = fibonacci(n-2); return dataflow( unwrapped([](uint64_t lhs, uint64_t rhs) { return lhs + rhs; }) , lhs_future, rhs_future ); }

. , "dataflow" await . future, . ++11 ++14 - .

- resumable ?
, , . Hartmut Kaiser , resumable . , \ . , .

resumable
- ( ) resumable . - , , . async/await, (if/else, for ..). . N3650 , std::future:

future<int> f(shared_ptr str) { shared_ptr<vector> buf = ...; return str->read(512, buf) .then([](future<int> op)// lambda 1 { return op.get() + 11; }); } future<void> g() { shared_ptr s = ...; return f(s).then([s](future<int> op) // lambda 2 { s->close(); }); }

resumable :

future<void> f(stream str) async { shared_ptr<vector> buf = ...; int count = await str.read(512, buf); return count + 11; } future g() async { stream s = ...; int pls11 = await f(s); s.close(); }

resumable (, , ). . , Herb Sutter BUILD:

std::string read( std::string file, std::string suffix ) { std::istream fi = open(file).get(); std::string ret, chunk; while( (chunk = fi.read().get()).size() ) ret += chunk + suffix; return ret; }

" " - future::get() std::future. , get() then(). , .

task<std::string> read( std::string file, std::string suffix ) { return open(file) .then([=](std::istream fi) { auto ret = std::make_shared<std::string>(); auto next = std::make_shared<std::function<task()>>( [=]{ fi.read() .then([=](std::string chunk) { if( chunk.size() ) { *ret += chunk + suffix; return (*next)(); } return *ret; }); }); return (*next)(); }); }

.then() . async/await:

task<std::string> read( std::string file, std::string suffix ) __async { std::istream fi = __await open(file); std::string ret, chunk; while( (chunk = __await fi.read()).size() ) ret += chunk + suffix; return ret; }

task<std::string> , . await , .then(). __async __await , Visual Studio.

. , - . std::future, auto .then , - , . resumable . , , , async/await. resumable , "" , std::future, 1:0 resumable .

. , resumable . , ++? . . . - 2 : . - ? . , . . ? ? futures (- ). , , , . resumable (, ) " ": , , , - " ".

, resumable . std::(shared_)future void. , boost::future hpx::future. , "", . - resumable VArgs, -. , variadic templates. , , resumable , , std::future - copy/move .


, ++14. -, ( ?) ++14 ( ) - . - ++11 . , - ++y1. resumable - (TS), - WG21. , resumable , . , . , CTP Visual Studio ( . : !). __async __await.

, . ++14 .then() future, await std::future::get.


. - , - . resumable . , Cilk ( - ). - resumable - - , .

( __async\__await ?). resumable STL Boost - .

, resumable - - (. ). , , . -, ? , , , - .

, , resumable . - resumable lambda functions ? - WG21, 2013 .

- N3722
resumable . , async resumable. , resumable "resumable", - . await .

, std::future . , s, :

get(), then(), callable object s, s& const s. get().

is_ready(), future


, s::promise_type, resumable . set_value(T) set_exception(exception_ptr). s::promise_type s.


, " " "". STL , , , - . - sequence , , , ( ).

yield:

sequence<int> range(int low, int high) resumable { for(int i = low; i <= high; ++i) { yield i; } }


yield i sequence. sequence , yield. - , , . yield await .
std::future.

resumable . await "" future, . await (, , "!").

. await - std::future, . , Hartmut Kaiser - :

std::future<uint64_t> fibonacci(uint64_t n) async { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs = std::async(&fibonacci, n-1); std::future<uint64_t> rhs = fibonacci(n-2); return await lhs + await rhs; }


resumable . lhs std::future , await, std::future .

, resumable - , await future . , , await. , await. , , .


resumable , " ?". - . , . resumable . Thomas Heller ( ) resumable .

std::future< uint64_t> fibonacci(uint64_t n) { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs_future = std::async(&fibonacci, n-1); //.unwrap(); std::future<uint64_t> rhs_future = fibonacci(n-2); return dataflow( unwrapped([](uint64_t lhs, uint64_t rhs) { return lhs + rhs; }) , lhs_future, rhs_future ); }

. , "dataflow" await . future, . ++11 ++14 - .

- resumable ?
, , . Hartmut Kaiser , resumable . , \ . , .

resumable
- ( ) resumable . - , , . async/await, (if/else, for ..). . N3650 , std::future:

future<int> f(shared_ptr str) { shared_ptr<vector> buf = ...; return str->read(512, buf) .then([](future<int> op)// lambda 1 { return op.get() + 11; }); } future<void> g() { shared_ptr s = ...; return f(s).then([s](future<int> op) // lambda 2 { s->close(); }); }

resumable :

future<void> f(stream str) async { shared_ptr<vector> buf = ...; int count = await str.read(512, buf); return count + 11; } future g() async { stream s = ...; int pls11 = await f(s); s.close(); }

resumable (, , ). . , Herb Sutter BUILD:

std::string read( std::string file, std::string suffix ) { std::istream fi = open(file).get(); std::string ret, chunk; while( (chunk = fi.read().get()).size() ) ret += chunk + suffix; return ret; }

" " - future::get() std::future. , get() then(). , .

task<std::string> read( std::string file, std::string suffix ) { return open(file) .then([=](std::istream fi) { auto ret = std::make_shared<std::string>(); auto next = std::make_shared<std::function<task()>>( [=]{ fi.read() .then([=](std::string chunk) { if( chunk.size() ) { *ret += chunk + suffix; return (*next)(); } return *ret; }); }); return (*next)(); }); }

.then() . async/await:

task<std::string> read( std::string file, std::string suffix ) __async { std::istream fi = __await open(file); std::string ret, chunk; while( (chunk = __await fi.read()).size() ) ret += chunk + suffix; return ret; }

task<std::string> , . await , .then(). __async __await , Visual Studio.

. , - . std::future, auto .then , - , . resumable . , , , async/await. resumable , "" , std::future, 1:0 resumable .

. , resumable . , ++? . . . - 2 : . - ? . , . . ? ? futures (- ). , , , . resumable (, ) " ": , , , - " ".

, resumable . std::(shared_)future void. , boost::future hpx::future. , "", . - resumable VArgs, -. , variadic templates. , , resumable , , std::future - copy/move .


, ++14. -, ( ?) ++14 ( ) - . - ++11 . , - ++y1. resumable - (TS), - WG21. , resumable , . , . , CTP Visual Studio ( . : !). __async __await.

, . ++14 .then() future, await std::future::get.


. - , - . resumable . , Cilk ( - ). - resumable - - , .

( __async\__await ?). resumable STL Boost - .

, resumable - - (. ). , , . -, ? , , , - .

, , resumable . - resumable lambda functions ? - WG21, 2013 .

- N3722
resumable . , async resumable. , resumable "resumable", - . await .

, std::future . , s, :

get(), then(), callable object s, s& const s. get().

is_ready(), future


, s::promise_type, resumable . set_value(T) set_exception(exception_ptr). s::promise_type s.


, " " "". STL , , , - . - sequence , , , ( ).

yield:

sequence<int> range(int low, int high) resumable { for(int i = low; i <= high; ++i) { yield i; } }


yield i sequence. sequence , yield. - , , . yield await .
std::future.

resumable . await "" future, . await (, , "!").

. await - std::future, . , Hartmut Kaiser - :

std::future<uint64_t> fibonacci(uint64_t n) async { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs = std::async(&fibonacci, n-1); std::future<uint64_t> rhs = fibonacci(n-2); return await lhs + await rhs; }


resumable . lhs std::future , await, std::future .

, resumable - , await future . , , await. , await. , , .


resumable , " ?". - . , . resumable . Thomas Heller ( ) resumable .

std::future< uint64_t> fibonacci(uint64_t n) { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs_future = std::async(&fibonacci, n-1); //.unwrap(); std::future<uint64_t> rhs_future = fibonacci(n-2); return dataflow( unwrapped([](uint64_t lhs, uint64_t rhs) { return lhs + rhs; }) , lhs_future, rhs_future ); }

. , "dataflow" await . future, . ++11 ++14 - .

- resumable ?
, , . Hartmut Kaiser , resumable . , \ . , .

resumable
- ( ) resumable . - , , . async/await, (if/else, for ..). . N3650 , std::future:

future<int> f(shared_ptr str) { shared_ptr<vector> buf = ...; return str->read(512, buf) .then([](future<int> op)// lambda 1 { return op.get() + 11; }); } future<void> g() { shared_ptr s = ...; return f(s).then([s](future<int> op) // lambda 2 { s->close(); }); }

resumable :

future<void> f(stream str) async { shared_ptr<vector> buf = ...; int count = await str.read(512, buf); return count + 11; } future g() async { stream s = ...; int pls11 = await f(s); s.close(); }

resumable (, , ). . , Herb Sutter BUILD:

std::string read( std::string file, std::string suffix ) { std::istream fi = open(file).get(); std::string ret, chunk; while( (chunk = fi.read().get()).size() ) ret += chunk + suffix; return ret; }

" " - future::get() std::future. , get() then(). , .

task<std::string> read( std::string file, std::string suffix ) { return open(file) .then([=](std::istream fi) { auto ret = std::make_shared<std::string>(); auto next = std::make_shared<std::function<task()>>( [=]{ fi.read() .then([=](std::string chunk) { if( chunk.size() ) { *ret += chunk + suffix; return (*next)(); } return *ret; }); }); return (*next)(); }); }

.then() . async/await:

task<std::string> read( std::string file, std::string suffix ) __async { std::istream fi = __await open(file); std::string ret, chunk; while( (chunk = __await fi.read()).size() ) ret += chunk + suffix; return ret; }

task<std::string> , . await , .then(). __async __await , Visual Studio.

. , - . std::future, auto .then , - , . resumable . , , , async/await. resumable , "" , std::future, 1:0 resumable .

. , resumable . , ++? . . . - 2 : . - ? . , . . ? ? futures (- ). , , , . resumable (, ) " ": , , , - " ".

, resumable . std::(shared_)future void. , boost::future hpx::future. , "", . - resumable VArgs, -. , variadic templates. , , resumable , , std::future - copy/move .


, ++14. -, ( ?) ++14 ( ) - . - ++11 . , - ++y1. resumable - (TS), - WG21. , resumable , . , . , CTP Visual Studio ( . : !). __async __await.

, . ++14 .then() future, await std::future::get.


. - , - . resumable . , Cilk ( - ). - resumable - - , .

( __async\__await ?). resumable STL Boost - .

, resumable - - (. ). , , . -, ? , , , - .

, , resumable . - resumable lambda functions ? - WG21, 2013 .

- N3722
resumable . , async resumable. , resumable "resumable", - . await .

, std::future . , s, :

get(), then(), callable object s, s& const s. get().

is_ready(), future


, s::promise_type, resumable . set_value(T) set_exception(exception_ptr). s::promise_type s.


, " " "". STL , , , - . - sequence , , , ( ).

yield:

sequence<int> range(int low, int high) resumable { for(int i = low; i <= high; ++i) { yield i; } }


yield i sequence. sequence , yield. - , , . yield await .
std::future.

resumable . await "" future, . await (, , "!").

. await - std::future, . , Hartmut Kaiser - :

std::future<uint64_t> fibonacci(uint64_t n) async { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs = std::async(&fibonacci, n-1); std::future<uint64_t> rhs = fibonacci(n-2); return await lhs + await rhs; }


resumable . lhs std::future , await, std::future .

, resumable - , await future . , , await. , await. , , .


resumable , " ?". - . , . resumable . Thomas Heller ( ) resumable .

std::future< uint64_t> fibonacci(uint64_t n) { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs_future = std::async(&fibonacci, n-1); //.unwrap(); std::future<uint64_t> rhs_future = fibonacci(n-2); return dataflow( unwrapped([](uint64_t lhs, uint64_t rhs) { return lhs + rhs; }) , lhs_future, rhs_future ); }

. , "dataflow" await . future, . ++11 ++14 - .

- resumable ?
, , . Hartmut Kaiser , resumable . , \ . , .

resumable
- ( ) resumable . - , , . async/await, (if/else, for ..). . N3650 , std::future:

future<int> f(shared_ptr str) { shared_ptr<vector> buf = ...; return str->read(512, buf) .then([](future<int> op)// lambda 1 { return op.get() + 11; }); } future<void> g() { shared_ptr s = ...; return f(s).then([s](future<int> op) // lambda 2 { s->close(); }); }

resumable :

future<void> f(stream str) async { shared_ptr<vector> buf = ...; int count = await str.read(512, buf); return count + 11; } future g() async { stream s = ...; int pls11 = await f(s); s.close(); }

resumable (, , ). . , Herb Sutter BUILD:

std::string read( std::string file, std::string suffix ) { std::istream fi = open(file).get(); std::string ret, chunk; while( (chunk = fi.read().get()).size() ) ret += chunk + suffix; return ret; }

" " - future::get() std::future. , get() then(). , .

task<std::string> read( std::string file, std::string suffix ) { return open(file) .then([=](std::istream fi) { auto ret = std::make_shared<std::string>(); auto next = std::make_shared<std::function<task()>>( [=]{ fi.read() .then([=](std::string chunk) { if( chunk.size() ) { *ret += chunk + suffix; return (*next)(); } return *ret; }); }); return (*next)(); }); }

.then() . async/await:

task<std::string> read( std::string file, std::string suffix ) __async { std::istream fi = __await open(file); std::string ret, chunk; while( (chunk = __await fi.read()).size() ) ret += chunk + suffix; return ret; }

task<std::string> , . await , .then(). __async __await , Visual Studio.

. , - . std::future, auto .then , - , . resumable . , , , async/await. resumable , "" , std::future, 1:0 resumable .

. , resumable . , ++? . . . - 2 : . - ? . , . . ? ? futures (- ). , , , . resumable (, ) " ": , , , - " ".

, resumable . std::(shared_)future void. , boost::future hpx::future. , "", . - resumable VArgs, -. , variadic templates. , , resumable , , std::future - copy/move .


, ++14. -, ( ?) ++14 ( ) - . - ++11 . , - ++y1. resumable - (TS), - WG21. , resumable , . , . , CTP Visual Studio ( . : !). __async __await.

, . ++14 .then() future, await std::future::get.


. - , - . resumable . , Cilk ( - ). - resumable - - , .

( __async\__await ?). resumable STL Boost - .

, resumable - - (. ). , , . -, ? , , , - .

, , resumable . - resumable lambda functions ? - WG21, 2013 .

- N3722
resumable . , async resumable. , resumable "resumable", - . await .

, std::future . , s, :

get(), then(), callable object s, s& const s. get().

is_ready(), future


, s::promise_type, resumable . set_value(T) set_exception(exception_ptr). s::promise_type s.


, " " "". STL , , , - . - sequence , , , ( ).

yield:

sequence<int> range(int low, int high) resumable { for(int i = low; i <= high; ++i) { yield i; } }


yield i sequence. sequence , yield. - , , . yield await .
std::future.

resumable . await "" future, . await (, , "!").

. await - std::future, . , Hartmut Kaiser - :

std::future<uint64_t> fibonacci(uint64_t n) async { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs = std::async(&fibonacci, n-1); std::future<uint64_t> rhs = fibonacci(n-2); return await lhs + await rhs; }


resumable . lhs std::future , await, std::future .

, resumable - , await future . , , await. , await. , , .


resumable , " ?". - . , . resumable . Thomas Heller ( ) resumable .

std::future< uint64_t> fibonacci(uint64_t n) { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs_future = std::async(&fibonacci, n-1); //.unwrap(); std::future<uint64_t> rhs_future = fibonacci(n-2); return dataflow( unwrapped([](uint64_t lhs, uint64_t rhs) { return lhs + rhs; }) , lhs_future, rhs_future ); }

. , "dataflow" await . future, . ++11 ++14 - .

- resumable ?
, , . Hartmut Kaiser , resumable . , \ . , .

resumable
- ( ) resumable . - , , . async/await, (if/else, for ..). . N3650 , std::future:

future<int> f(shared_ptr str) { shared_ptr<vector> buf = ...; return str->read(512, buf) .then([](future<int> op)// lambda 1 { return op.get() + 11; }); } future<void> g() { shared_ptr s = ...; return f(s).then([s](future<int> op) // lambda 2 { s->close(); }); }

resumable :

future<void> f(stream str) async { shared_ptr<vector> buf = ...; int count = await str.read(512, buf); return count + 11; } future g() async { stream s = ...; int pls11 = await f(s); s.close(); }

resumable (, , ). . , Herb Sutter BUILD:

std::string read( std::string file, std::string suffix ) { std::istream fi = open(file).get(); std::string ret, chunk; while( (chunk = fi.read().get()).size() ) ret += chunk + suffix; return ret; }

" " - future::get() std::future. , get() then(). , .

task<std::string> read( std::string file, std::string suffix ) { return open(file) .then([=](std::istream fi) { auto ret = std::make_shared<std::string>(); auto next = std::make_shared<std::function<task()>>( [=]{ fi.read() .then([=](std::string chunk) { if( chunk.size() ) { *ret += chunk + suffix; return (*next)(); } return *ret; }); }); return (*next)(); }); }

.then() . async/await:

task<std::string> read( std::string file, std::string suffix ) __async { std::istream fi = __await open(file); std::string ret, chunk; while( (chunk = __await fi.read()).size() ) ret += chunk + suffix; return ret; }

task<std::string> , . await , .then(). __async __await , Visual Studio.

. , - . std::future, auto .then , - , . resumable . , , , async/await. resumable , "" , std::future, 1:0 resumable .

. , resumable . , ++? . . . - 2 : . - ? . , . . ? ? futures (- ). , , , . resumable (, ) " ": , , , - " ".

, resumable . std::(shared_)future void. , boost::future hpx::future. , "", . - resumable VArgs, -. , variadic templates. , , resumable , , std::future - copy/move .


, ++14. -, ( ?) ++14 ( ) - . - ++11 . , - ++y1. resumable - (TS), - WG21. , resumable , . , . , CTP Visual Studio ( . : !). __async __await.

, . ++14 .then() future, await std::future::get.


. - , - . resumable . , Cilk ( - ). - resumable - - , .

( __async\__await ?). resumable STL Boost - .

, resumable - - (. ). , , . -, ? , , , - .

, , resumable . - resumable lambda functions ? - WG21, 2013 .

- N3722
resumable . , async resumable. , resumable "resumable", - . await .

, std::future . , s, :

get(), then(), callable object s, s& const s. get().

is_ready(), future


, s::promise_type, resumable . set_value(T) set_exception(exception_ptr). s::promise_type s.


, " " "". STL , , , - . - sequence , , , ( ).

yield:

sequence<int> range(int low, int high) resumable { for(int i = low; i <= high; ++i) { yield i; } }


yield i sequence. sequence , yield. - , , . yield await .
std::future.

resumable . await "" future, . await (, , "!").

. await - std::future, . , Hartmut Kaiser - :

std::future<uint64_t> fibonacci(uint64_t n) async { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs = std::async(&fibonacci, n-1); std::future<uint64_t> rhs = fibonacci(n-2); return await lhs + await rhs; }


resumable . lhs std::future , await, std::future .

, resumable - , await future . , , await. , await. , , .


resumable , " ?". - . , . resumable . Thomas Heller ( ) resumable .

std::future< uint64_t> fibonacci(uint64_t n) { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs_future = std::async(&fibonacci, n-1); //.unwrap(); std::future<uint64_t> rhs_future = fibonacci(n-2); return dataflow( unwrapped([](uint64_t lhs, uint64_t rhs) { return lhs + rhs; }) , lhs_future, rhs_future ); }

. , "dataflow" await . future, . ++11 ++14 - .

- resumable ?
, , . Hartmut Kaiser , resumable . , \ . , .

resumable
- ( ) resumable . - , , . async/await, (if/else, for ..). . N3650 , std::future:

future<int> f(shared_ptr str) { shared_ptr<vector> buf = ...; return str->read(512, buf) .then([](future<int> op)// lambda 1 { return op.get() + 11; }); } future<void> g() { shared_ptr s = ...; return f(s).then([s](future<int> op) // lambda 2 { s->close(); }); }

resumable :

future<void> f(stream str) async { shared_ptr<vector> buf = ...; int count = await str.read(512, buf); return count + 11; } future g() async { stream s = ...; int pls11 = await f(s); s.close(); }

resumable (, , ). . , Herb Sutter BUILD:

std::string read( std::string file, std::string suffix ) { std::istream fi = open(file).get(); std::string ret, chunk; while( (chunk = fi.read().get()).size() ) ret += chunk + suffix; return ret; }

" " - future::get() std::future. , get() then(). , .

task<std::string> read( std::string file, std::string suffix ) { return open(file) .then([=](std::istream fi) { auto ret = std::make_shared<std::string>(); auto next = std::make_shared<std::function<task()>>( [=]{ fi.read() .then([=](std::string chunk) { if( chunk.size() ) { *ret += chunk + suffix; return (*next)(); } return *ret; }); }); return (*next)(); }); }

.then() . async/await:

task<std::string> read( std::string file, std::string suffix ) __async { std::istream fi = __await open(file); std::string ret, chunk; while( (chunk = __await fi.read()).size() ) ret += chunk + suffix; return ret; }

task<std::string> , . await , .then(). __async __await , Visual Studio.

. , - . std::future, auto .then , - , . resumable . , , , async/await. resumable , "" , std::future, 1:0 resumable .

. , resumable . , ++? . . . - 2 : . - ? . , . . ? ? futures (- ). , , , . resumable (, ) " ": , , , - " ".

, resumable . std::(shared_)future void. , boost::future hpx::future. , "", . - resumable VArgs, -. , variadic templates. , , resumable , , std::future - copy/move .


, ++14. -, ( ?) ++14 ( ) - . - ++11 . , - ++y1. resumable - (TS), - WG21. , resumable , . , . , CTP Visual Studio ( . : !). __async __await.

, . ++14 .then() future, await std::future::get.


. - , - . resumable . , Cilk ( - ). - resumable - - , .

( __async\__await ?). resumable STL Boost - .

, resumable - - (. ). , , . -, ? , , , - .

, , resumable . - resumable lambda functions ? - WG21, 2013 .

- N3722
resumable . , async resumable. , resumable "resumable", - . await .

, std::future . , s, :

get(), then(), callable object s, s& const s. get().

is_ready(), future


, s::promise_type, resumable . set_value(T) set_exception(exception_ptr). s::promise_type s.


, " " "". STL , , , - . - sequence , , , ( ).

yield:

sequence<int> range(int low, int high) resumable { for(int i = low; i <= high; ++i) { yield i; } }


yield i sequence. sequence , yield. - , , . yield await .
 std::future. 

resumable . await "" future, . await (, , "!").

. await - std::future, . , Hartmut Kaiser - :

std::future<uint64_t> fibonacci(uint64_t n) async { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs = std::async(&fibonacci, n-1); std::future<uint64_t> rhs = fibonacci(n-2); return await lhs + await rhs; }


resumable . lhs std::future , await, std::future .

, resumable - , await future . , , await. , await. , , .


resumable , " ?". - . , . resumable . Thomas Heller ( ) resumable .

std::future< uint64_t> fibonacci(uint64_t n) { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs_future = std::async(&fibonacci, n-1); //.unwrap(); std::future<uint64_t> rhs_future = fibonacci(n-2); return dataflow( unwrapped([](uint64_t lhs, uint64_t rhs) { return lhs + rhs; }) , lhs_future, rhs_future ); }

. , "dataflow" await . future, . ++11 ++14 - .

- resumable ?
, , . Hartmut Kaiser , resumable . , \ . , .

resumable
- ( ) resumable . - , , . async/await, (if/else, for ..). . N3650 , std::future:

future<int> f(shared_ptr str) { shared_ptr<vector> buf = ...; return str->read(512, buf) .then([](future<int> op)// lambda 1 { return op.get() + 11; }); } future<void> g() { shared_ptr s = ...; return f(s).then([s](future<int> op) // lambda 2 { s->close(); }); }

resumable :

future<void> f(stream str) async { shared_ptr<vector> buf = ...; int count = await str.read(512, buf); return count + 11; } future g() async { stream s = ...; int pls11 = await f(s); s.close(); }

resumable (, , ). . , Herb Sutter BUILD:

std::string read( std::string file, std::string suffix ) { std::istream fi = open(file).get(); std::string ret, chunk; while( (chunk = fi.read().get()).size() ) ret += chunk + suffix; return ret; }

" " - future::get() std::future. , get() then(). , .

task<std::string> read( std::string file, std::string suffix ) { return open(file) .then([=](std::istream fi) { auto ret = std::make_shared<std::string>(); auto next = std::make_shared<std::function<task()>>( [=]{ fi.read() .then([=](std::string chunk) { if( chunk.size() ) { *ret += chunk + suffix; return (*next)(); } return *ret; }); }); return (*next)(); }); }

.then() . async/await:

task<std::string> read( std::string file, std::string suffix ) __async { std::istream fi = __await open(file); std::string ret, chunk; while( (chunk = __await fi.read()).size() ) ret += chunk + suffix; return ret; }

task<std::string> , . await , .then(). __async __await , Visual Studio.

. , - . std::future, auto .then , - , . resumable . , , , async/await. resumable , "" , std::future, 1:0 resumable .

. , resumable . , ++? . . . - 2 : . - ? . , . . ? ? futures (- ). , , , . resumable (, ) " ": , , , - " ".

, resumable . std::(shared_)future void. , boost::future hpx::future. , "", . - resumable VArgs, -. , variadic templates. , , resumable , , std::future - copy/move .


, ++14. -, ( ?) ++14 ( ) - . - ++11 . , - ++y1. resumable - (TS), - WG21. , resumable , . , . , CTP Visual Studio ( . : !). __async __await.

, . ++14 .then() future, await std::future::get.


. - , - . resumable . , Cilk ( - ). - resumable - - , .

( __async\__await ?). resumable STL Boost - .

, resumable - - (. ). , , . -, ? , , , - .

, , resumable . - resumable lambda functions ? - WG21, 2013 .

- N3722
resumable . , async resumable. , resumable "resumable", - . await .

, std::future . , s, :

get(), then(), callable object s, s& const s. get().

is_ready(), future


, s::promise_type, resumable . set_value(T) set_exception(exception_ptr). s::promise_type s.


, " " "". STL , , , - . - sequence , , , ( ).

yield:

sequence<int> range(int low, int high) resumable { for(int i = low; i <= high; ++i) { yield i; } }


yield i sequence. sequence , yield. - , , . yield await .
std::future.

resumable . await "" future, . await (, , "!").

. await - std::future, . , Hartmut Kaiser - :

std::future<uint64_t> fibonacci(uint64_t n) async { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs = std::async(&fibonacci, n-1); std::future<uint64_t> rhs = fibonacci(n-2); return await lhs + await rhs; }


resumable . lhs std::future , await, std::future .

, resumable - , await future . , , await. , await. , , .


resumable , " ?". - . , . resumable . Thomas Heller ( ) resumable .

std::future< uint64_t> fibonacci(uint64_t n) { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs_future = std::async(&fibonacci, n-1); //.unwrap(); std::future<uint64_t> rhs_future = fibonacci(n-2); return dataflow( unwrapped([](uint64_t lhs, uint64_t rhs) { return lhs + rhs; }) , lhs_future, rhs_future ); }

. , "dataflow" await . future, . ++11 ++14 - .

- resumable ?
, , . Hartmut Kaiser , resumable . , \ . , .

resumable
- ( ) resumable . - , , . async/await, (if/else, for ..). . N3650 , std::future:

future<int> f(shared_ptr str) { shared_ptr<vector> buf = ...; return str->read(512, buf) .then([](future<int> op)// lambda 1 { return op.get() + 11; }); } future<void> g() { shared_ptr s = ...; return f(s).then([s](future<int> op) // lambda 2 { s->close(); }); }

resumable :

future<void> f(stream str) async { shared_ptr<vector> buf = ...; int count = await str.read(512, buf); return count + 11; } future g() async { stream s = ...; int pls11 = await f(s); s.close(); }

resumable (, , ). . , Herb Sutter BUILD:

std::string read( std::string file, std::string suffix ) { std::istream fi = open(file).get(); std::string ret, chunk; while( (chunk = fi.read().get()).size() ) ret += chunk + suffix; return ret; }

" " - future::get() std::future. , get() then(). , .

task<std::string> read( std::string file, std::string suffix ) { return open(file) .then([=](std::istream fi) { auto ret = std::make_shared<std::string>(); auto next = std::make_shared<std::function<task()>>( [=]{ fi.read() .then([=](std::string chunk) { if( chunk.size() ) { *ret += chunk + suffix; return (*next)(); } return *ret; }); }); return (*next)(); }); }

.then() . async/await:

task<std::string> read( std::string file, std::string suffix ) __async { std::istream fi = __await open(file); std::string ret, chunk; while( (chunk = __await fi.read()).size() ) ret += chunk + suffix; return ret; }

task<std::string> , . await , .then(). __async __await , Visual Studio.

. , - . std::future, auto .then , - , . resumable . , , , async/await. resumable , "" , std::future, 1:0 resumable .

. , resumable . , ++? . . . - 2 : . - ? . , . . ? ? futures (- ). , , , . resumable (, ) " ": , , , - " ".

, resumable . std::(shared_)future void. , boost::future hpx::future. , "", . - resumable VArgs, -. , variadic templates. , , resumable , , std::future - copy/move .


, ++14. -, ( ?) ++14 ( ) - . - ++11 . , - ++y1. resumable - (TS), - WG21. , resumable , . , . , CTP Visual Studio ( . : !). __async __await.

, . ++14 .then() future, await std::future::get.


. - , - . resumable . , Cilk ( - ). - resumable - - , .

( __async\__await ?). resumable STL Boost - .

, resumable - - (. ). , , . -, ? , , , - .

, , resumable . - resumable lambda functions ? - WG21, 2013 .

- N3722
resumable . , async resumable. , resumable "resumable", - . await .

, std::future . , s, :

get(), then(), callable object s, s& const s. get().

is_ready(), future


, s::promise_type, resumable . set_value(T) set_exception(exception_ptr). s::promise_type s.


, " " "". STL , , , - . - sequence , , , ( ).

yield:

sequence<int> range(int low, int high) resumable { for(int i = low; i <= high; ++i) { yield i; } }


yield i sequence. sequence , yield. - , , . yield await .
 std::future. 

resumable . await "" future, . await (, , "!").

. await - std::future, . , Hartmut Kaiser - :

std::future<uint64_t> fibonacci(uint64_t n) async { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs = std::async(&fibonacci, n-1); std::future<uint64_t> rhs = fibonacci(n-2); return await lhs + await rhs; }


resumable . lhs std::future , await, std::future .

, resumable - , await future . , , await. , await. , , .


resumable , " ?". - . , . resumable . Thomas Heller ( ) resumable .

std::future< uint64_t> fibonacci(uint64_t n) { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs_future = std::async(&fibonacci, n-1); //.unwrap(); std::future<uint64_t> rhs_future = fibonacci(n-2); return dataflow( unwrapped([](uint64_t lhs, uint64_t rhs) { return lhs + rhs; }) , lhs_future, rhs_future ); }

. , "dataflow" await . future, . ++11 ++14 - .

- resumable ?
, , . Hartmut Kaiser , resumable . , \ . , .

resumable
- ( ) resumable . - , , . async/await, (if/else, for ..). . N3650 , std::future:

future<int> f(shared_ptr str) { shared_ptr<vector> buf = ...; return str->read(512, buf) .then([](future<int> op)// lambda 1 { return op.get() + 11; }); } future<void> g() { shared_ptr s = ...; return f(s).then([s](future<int> op) // lambda 2 { s->close(); }); }

resumable :

future<void> f(stream str) async { shared_ptr<vector> buf = ...; int count = await str.read(512, buf); return count + 11; } future g() async { stream s = ...; int pls11 = await f(s); s.close(); }

resumable (, , ). . , Herb Sutter BUILD:

std::string read( std::string file, std::string suffix ) { std::istream fi = open(file).get(); std::string ret, chunk; while( (chunk = fi.read().get()).size() ) ret += chunk + suffix; return ret; }

" " - future::get() std::future. , get() then(). , .

task<std::string> read( std::string file, std::string suffix ) { return open(file) .then([=](std::istream fi) { auto ret = std::make_shared<std::string>(); auto next = std::make_shared<std::function<task()>>( [=]{ fi.read() .then([=](std::string chunk) { if( chunk.size() ) { *ret += chunk + suffix; return (*next)(); } return *ret; }); }); return (*next)(); }); }

.then() . async/await:

task<std::string> read( std::string file, std::string suffix ) __async { std::istream fi = __await open(file); std::string ret, chunk; while( (chunk = __await fi.read()).size() ) ret += chunk + suffix; return ret; }

task<std::string> , . await , .then(). __async __await , Visual Studio.

. , - . std::future, auto .then , - , . resumable . , , , async/await. resumable , "" , std::future, 1:0 resumable .

. , resumable . , ++? . . . - 2 : . - ? . , . . ? ? futures (- ). , , , . resumable (, ) " ": , , , - " ".

, resumable . std::(shared_)future void. , boost::future hpx::future. , "", . - resumable VArgs, -. , variadic templates. , , resumable , , std::future - copy/move .


, ++14. -, ( ?) ++14 ( ) - . - ++11 . , - ++y1. resumable - (TS), - WG21. , resumable , . , . , CTP Visual Studio ( . : !). __async __await.

, . ++14 .then() future, await std::future::get.


. - , - . resumable . , Cilk ( - ). - resumable - - , .

( __async\__await ?). resumable STL Boost - .

, resumable - - (. ). , , . -, ? , , , - .

, , resumable . - resumable lambda functions ? - WG21, 2013 .

- N3722
resumable . , async resumable. , resumable "resumable", - . await .

, std::future . , s, :

get(), then(), callable object s, s& const s. get().

is_ready(), future


, s::promise_type, resumable . set_value(T) set_exception(exception_ptr). s::promise_type s.


, " " "". STL , , , - . - sequence , , , ( ).

yield:

sequence<int> range(int low, int high) resumable { for(int i = low; i <= high; ++i) { yield i; } }


yield i sequence. sequence , yield. - , , . yield await .
std::future.

resumable . await "" future, . await (, , "!").

. await - std::future, . , Hartmut Kaiser - :

std::future<uint64_t> fibonacci(uint64_t n) async { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs = std::async(&fibonacci, n-1); std::future<uint64_t> rhs = fibonacci(n-2); return await lhs + await rhs; }


resumable . lhs std::future , await, std::future .

, resumable - , await future . , , await. , await. , , .


resumable , " ?". - . , . resumable . Thomas Heller ( ) resumable .

std::future< uint64_t> fibonacci(uint64_t n) { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs_future = std::async(&fibonacci, n-1); //.unwrap(); std::future<uint64_t> rhs_future = fibonacci(n-2); return dataflow( unwrapped([](uint64_t lhs, uint64_t rhs) { return lhs + rhs; }) , lhs_future, rhs_future ); }

. , "dataflow" await . future, . ++11 ++14 - .

- resumable ?
, , . Hartmut Kaiser , resumable . , \ . , .

resumable
- ( ) resumable . - , , . async/await, (if/else, for ..). . N3650 , std::future:

future<int> f(shared_ptr str) { shared_ptr<vector> buf = ...; return str->read(512, buf) .then([](future<int> op)// lambda 1 { return op.get() + 11; }); } future<void> g() { shared_ptr s = ...; return f(s).then([s](future<int> op) // lambda 2 { s->close(); }); }

resumable :

future<void> f(stream str) async { shared_ptr<vector> buf = ...; int count = await str.read(512, buf); return count + 11; } future g() async { stream s = ...; int pls11 = await f(s); s.close(); }

resumable (, , ). . , Herb Sutter BUILD:

std::string read( std::string file, std::string suffix ) { std::istream fi = open(file).get(); std::string ret, chunk; while( (chunk = fi.read().get()).size() ) ret += chunk + suffix; return ret; }

" " - future::get() std::future. , get() then(). , .

task<std::string> read( std::string file, std::string suffix ) { return open(file) .then([=](std::istream fi) { auto ret = std::make_shared<std::string>(); auto next = std::make_shared<std::function<task()>>( [=]{ fi.read() .then([=](std::string chunk) { if( chunk.size() ) { *ret += chunk + suffix; return (*next)(); } return *ret; }); }); return (*next)(); }); }

.then() . async/await:

task<std::string> read( std::string file, std::string suffix ) __async { std::istream fi = __await open(file); std::string ret, chunk; while( (chunk = __await fi.read()).size() ) ret += chunk + suffix; return ret; }

task<std::string> , . await , .then(). __async __await , Visual Studio.

. , - . std::future, auto .then , - , . resumable . , , , async/await. resumable , "" , std::future, 1:0 resumable .

. , resumable . , ++? . . . - 2 : . - ? . , . . ? ? futures (- ). , , , . resumable (, ) " ": , , , - " ".

, resumable . std::(shared_)future void. , boost::future hpx::future. , "", . - resumable VArgs, -. , variadic templates. , , resumable , , std::future - copy/move .


, ++14. -, ( ?) ++14 ( ) - . - ++11 . , - ++y1. resumable - (TS), - WG21. , resumable , . , . , CTP Visual Studio ( . : !). __async __await.

, . ++14 .then() future, await std::future::get.


. - , - . resumable . , Cilk ( - ). - resumable - - , .

( __async\__await ?). resumable STL Boost - .

, resumable - - (. ). , , . -, ? , , , - .

, , resumable . - resumable lambda functions ? - WG21, 2013 .

- N3722
resumable . , async resumable. , resumable "resumable", - . await .

, std::future . , s, :

get(), then(), callable object s, s& const s. get().

is_ready(), future


, s::promise_type, resumable . set_value(T) set_exception(exception_ptr). s::promise_type s.


, " " "". STL , , , - . - sequence , , , ( ).

yield:

sequence<int> range(int low, int high) resumable { for(int i = low; i <= high; ++i) { yield i; } }


yield i sequence. sequence , yield. - , , . yield await .
 std::future. 

resumable . await "" future, . await (, , "!").

. await - std::future, . , Hartmut Kaiser - :

std::future<uint64_t> fibonacci(uint64_t n) async { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs = std::async(&fibonacci, n-1); std::future<uint64_t> rhs = fibonacci(n-2); return await lhs + await rhs; }


resumable . lhs std::future , await, std::future .

, resumable - , await future . , , await. , await. , , .


resumable , " ?". - . , . resumable . Thomas Heller ( ) resumable .

std::future< uint64_t> fibonacci(uint64_t n) { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs_future = std::async(&fibonacci, n-1); //.unwrap(); std::future<uint64_t> rhs_future = fibonacci(n-2); return dataflow( unwrapped([](uint64_t lhs, uint64_t rhs) { return lhs + rhs; }) , lhs_future, rhs_future ); }

. , "dataflow" await . future, . ++11 ++14 - .

- resumable ?
, , . Hartmut Kaiser , resumable . , \ . , .

resumable
- ( ) resumable . - , , . async/await, (if/else, for ..). . N3650 , std::future:

future<int> f(shared_ptr str) { shared_ptr<vector> buf = ...; return str->read(512, buf) .then([](future<int> op)// lambda 1 { return op.get() + 11; }); } future<void> g() { shared_ptr s = ...; return f(s).then([s](future<int> op) // lambda 2 { s->close(); }); }

resumable :

future<void> f(stream str) async { shared_ptr<vector> buf = ...; int count = await str.read(512, buf); return count + 11; } future g() async { stream s = ...; int pls11 = await f(s); s.close(); }

resumable (, , ). . , Herb Sutter BUILD:

std::string read( std::string file, std::string suffix ) { std::istream fi = open(file).get(); std::string ret, chunk; while( (chunk = fi.read().get()).size() ) ret += chunk + suffix; return ret; }

" " - future::get() std::future. , get() then(). , .

task<std::string> read( std::string file, std::string suffix ) { return open(file) .then([=](std::istream fi) { auto ret = std::make_shared<std::string>(); auto next = std::make_shared<std::function<task()>>( [=]{ fi.read() .then([=](std::string chunk) { if( chunk.size() ) { *ret += chunk + suffix; return (*next)(); } return *ret; }); }); return (*next)(); }); }

.then() . async/await:

task<std::string> read( std::string file, std::string suffix ) __async { std::istream fi = __await open(file); std::string ret, chunk; while( (chunk = __await fi.read()).size() ) ret += chunk + suffix; return ret; }

task<std::string> , . await , .then(). __async __await , Visual Studio.

. , - . std::future, auto .then , - , . resumable . , , , async/await. resumable , "" , std::future, 1:0 resumable .

. , resumable . , ++? . . . - 2 : . - ? . , . . ? ? futures (- ). , , , . resumable (, ) " ": , , , - " ".

, resumable . std::(shared_)future void. , boost::future hpx::future. , "", . - resumable VArgs, -. , variadic templates. , , resumable , , std::future - copy/move .


, ++14. -, ( ?) ++14 ( ) - . - ++11 . , - ++y1. resumable - (TS), - WG21. , resumable , . , . , CTP Visual Studio ( . : !). __async __await.

, . ++14 .then() future, await std::future::get.


. - , - . resumable . , Cilk ( - ). - resumable - - , .

( __async\__await ?). resumable STL Boost - .

, resumable - - (. ). , , . -, ? , , , - .

, , resumable . - resumable lambda functions ? - WG21, 2013 .

- N3722
resumable . , async resumable. , resumable "resumable", - . await .

, std::future . , s, :

get(), then(), callable object s, s& const s. get().

is_ready(), future


, s::promise_type, resumable . set_value(T) set_exception(exception_ptr). s::promise_type s.


, " " "". STL , , , - . - sequence , , , ( ).

yield:

sequence<int> range(int low, int high) resumable { for(int i = low; i <= high; ++i) { yield i; } }


yield i sequence. sequence , yield. - , , . yield await .
std::future.

resumable . await "" future, . await (, , "!").

. await - std::future, . , Hartmut Kaiser - :

std::future<uint64_t> fibonacci(uint64_t n) async { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs = std::async(&fibonacci, n-1); std::future<uint64_t> rhs = fibonacci(n-2); return await lhs + await rhs; }


resumable . lhs std::future , await, std::future .

, resumable - , await future . , , await. , await. , , .


resumable , " ?". - . , . resumable . Thomas Heller ( ) resumable .

std::future< uint64_t> fibonacci(uint64_t n) { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs_future = std::async(&fibonacci, n-1); //.unwrap(); std::future<uint64_t> rhs_future = fibonacci(n-2); return dataflow( unwrapped([](uint64_t lhs, uint64_t rhs) { return lhs + rhs; }) , lhs_future, rhs_future ); }

. , "dataflow" await . future, . ++11 ++14 - .

- resumable ?
, , . Hartmut Kaiser , resumable . , \ . , .

resumable
- ( ) resumable . - , , . async/await, (if/else, for ..). . N3650 , std::future:

future<int> f(shared_ptr str) { shared_ptr<vector> buf = ...; return str->read(512, buf) .then([](future<int> op)// lambda 1 { return op.get() + 11; }); } future<void> g() { shared_ptr s = ...; return f(s).then([s](future<int> op) // lambda 2 { s->close(); }); }

resumable :

future<void> f(stream str) async { shared_ptr<vector> buf = ...; int count = await str.read(512, buf); return count + 11; } future g() async { stream s = ...; int pls11 = await f(s); s.close(); }

resumable (, , ). . , Herb Sutter BUILD:

std::string read( std::string file, std::string suffix ) { std::istream fi = open(file).get(); std::string ret, chunk; while( (chunk = fi.read().get()).size() ) ret += chunk + suffix; return ret; }

" " - future::get() std::future. , get() then(). , .

task<std::string> read( std::string file, std::string suffix ) { return open(file) .then([=](std::istream fi) { auto ret = std::make_shared<std::string>(); auto next = std::make_shared<std::function<task()>>( [=]{ fi.read() .then([=](std::string chunk) { if( chunk.size() ) { *ret += chunk + suffix; return (*next)(); } return *ret; }); }); return (*next)(); }); }

.then() . async/await:

task<std::string> read( std::string file, std::string suffix ) __async { std::istream fi = __await open(file); std::string ret, chunk; while( (chunk = __await fi.read()).size() ) ret += chunk + suffix; return ret; }

task<std::string> , . await , .then(). __async __await , Visual Studio.

. , - . std::future, auto .then , - , . resumable . , , , async/await. resumable , "" , std::future, 1:0 resumable .

. , resumable . , ++? . . . - 2 : . - ? . , . . ? ? futures (- ). , , , . resumable (, ) " ": , , , - " ".

, resumable . std::(shared_)future void. , boost::future hpx::future. , "", . - resumable VArgs, -. , variadic templates. , , resumable , , std::future - copy/move .


, ++14. -, ( ?) ++14 ( ) - . - ++11 . , - ++y1. resumable - (TS), - WG21. , resumable , . , . , CTP Visual Studio ( . : !). __async __await.

, . ++14 .then() future, await std::future::get.


. - , - . resumable . , Cilk ( - ). - resumable - - , .

( __async\__await ?). resumable STL Boost - .

, resumable - - (. ). , , . -, ? , , , - .

, , resumable . - resumable lambda functions ? - WG21, 2013 .

- N3722
resumable . , async resumable. , resumable "resumable", - . await .

, std::future . , s, :

get(), then(), callable object s, s& const s. get().

is_ready(), future


, s::promise_type, resumable . set_value(T) set_exception(exception_ptr). s::promise_type s.


, " " "". STL , , , - . - sequence , , , ( ).

yield:

sequence<int> range(int low, int high) resumable { for(int i = low; i <= high; ++i) { yield i; } }


yield i sequence. sequence , yield. - , , . yield await .
 std::future. 

resumable . await "" future, . await (, , "!").

. await - std::future, . , Hartmut Kaiser - :

std::future<uint64_t> fibonacci(uint64_t n) async { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs = std::async(&fibonacci, n-1); std::future<uint64_t> rhs = fibonacci(n-2); return await lhs + await rhs; }


resumable . lhs std::future , await, std::future .

, resumable - , await future . , , await. , await. , , .


resumable , " ?". - . , . resumable . Thomas Heller ( ) resumable .

std::future< uint64_t> fibonacci(uint64_t n) { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs_future = std::async(&fibonacci, n-1); //.unwrap(); std::future<uint64_t> rhs_future = fibonacci(n-2); return dataflow( unwrapped([](uint64_t lhs, uint64_t rhs) { return lhs + rhs; }) , lhs_future, rhs_future ); }

. , "dataflow" await . future, . ++11 ++14 - .

- resumable ?
, , . Hartmut Kaiser , resumable . , \ . , .

resumable
- ( ) resumable . - , , . async/await, (if/else, for ..). . N3650 , std::future:

future<int> f(shared_ptr str) { shared_ptr<vector> buf = ...; return str->read(512, buf) .then([](future<int> op)// lambda 1 { return op.get() + 11; }); } future<void> g() { shared_ptr s = ...; return f(s).then([s](future<int> op) // lambda 2 { s->close(); }); }

resumable :

future<void> f(stream str) async { shared_ptr<vector> buf = ...; int count = await str.read(512, buf); return count + 11; } future g() async { stream s = ...; int pls11 = await f(s); s.close(); }

resumable (, , ). . , Herb Sutter BUILD:

std::string read( std::string file, std::string suffix ) { std::istream fi = open(file).get(); std::string ret, chunk; while( (chunk = fi.read().get()).size() ) ret += chunk + suffix; return ret; }

" " - future::get() std::future. , get() then(). , .

task<std::string> read( std::string file, std::string suffix ) { return open(file) .then([=](std::istream fi) { auto ret = std::make_shared<std::string>(); auto next = std::make_shared<std::function<task()>>( [=]{ fi.read() .then([=](std::string chunk) { if( chunk.size() ) { *ret += chunk + suffix; return (*next)(); } return *ret; }); }); return (*next)(); }); }

.then() . async/await:

task<std::string> read( std::string file, std::string suffix ) __async { std::istream fi = __await open(file); std::string ret, chunk; while( (chunk = __await fi.read()).size() ) ret += chunk + suffix; return ret; }

task<std::string> , . await , .then(). __async __await , Visual Studio.

. , - . std::future, auto .then , - , . resumable . , , , async/await. resumable , "" , std::future, 1:0 resumable .

. , resumable . , ++? . . . - 2 : . - ? . , . . ? ? futures (- ). , , , . resumable (, ) " ": , , , - " ".

, resumable . std::(shared_)future void. , boost::future hpx::future. , "", . - resumable VArgs, -. , variadic templates. , , resumable , , std::future - copy/move .


, ++14. -, ( ?) ++14 ( ) - . - ++11 . , - ++y1. resumable - (TS), - WG21. , resumable , . , . , CTP Visual Studio ( . : !). __async __await.

, . ++14 .then() future, await std::future::get.


. - , - . resumable . , Cilk ( - ). - resumable - - , .

( __async\__await ?). resumable STL Boost - .

, resumable - - (. ). , , . -, ? , , , - .

, , resumable . - resumable lambda functions ? - WG21, 2013 .

- N3722
resumable . , async resumable. , resumable "resumable", - . await .

, std::future . , s, :

get(), then(), callable object s, s& const s. get().

is_ready(), future


, s::promise_type, resumable . set_value(T) set_exception(exception_ptr). s::promise_type s.


, " " "". STL , , , - . - sequence , , , ( ).

yield:

sequence<int> range(int low, int high) resumable { for(int i = low; i <= high; ++i) { yield i; } }


yield i sequence. sequence , yield. - , , . yield await .
std::future.

resumable . await "" future, . await (, , "!").

. await - std::future, . , Hartmut Kaiser - :

std::future<uint64_t> fibonacci(uint64_t n) async { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs = std::async(&fibonacci, n-1); std::future<uint64_t> rhs = fibonacci(n-2); return await lhs + await rhs; }


resumable . lhs std::future , await, std::future .

, resumable - , await future . , , await. , await. , , .


resumable , " ?". - . , . resumable . Thomas Heller ( ) resumable .

std::future< uint64_t> fibonacci(uint64_t n) { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs_future = std::async(&fibonacci, n-1); //.unwrap(); std::future<uint64_t> rhs_future = fibonacci(n-2); return dataflow( unwrapped([](uint64_t lhs, uint64_t rhs) { return lhs + rhs; }) , lhs_future, rhs_future ); }

. , "dataflow" await . future, . ++11 ++14 - .

- resumable ?
, , . Hartmut Kaiser , resumable . , \ . , .

resumable
- ( ) resumable . - , , . async/await, (if/else, for ..). . N3650 , std::future:

future<int> f(shared_ptr str) { shared_ptr<vector> buf = ...; return str->read(512, buf) .then([](future<int> op)// lambda 1 { return op.get() + 11; }); } future<void> g() { shared_ptr s = ...; return f(s).then([s](future<int> op) // lambda 2 { s->close(); }); }

resumable :

future<void> f(stream str) async { shared_ptr<vector> buf = ...; int count = await str.read(512, buf); return count + 11; } future g() async { stream s = ...; int pls11 = await f(s); s.close(); }

resumable (, , ). . , Herb Sutter BUILD:

std::string read( std::string file, std::string suffix ) { std::istream fi = open(file).get(); std::string ret, chunk; while( (chunk = fi.read().get()).size() ) ret += chunk + suffix; return ret; }

" " - future::get() std::future. , get() then(). , .

task<std::string> read( std::string file, std::string suffix ) { return open(file) .then([=](std::istream fi) { auto ret = std::make_shared<std::string>(); auto next = std::make_shared<std::function<task()>>( [=]{ fi.read() .then([=](std::string chunk) { if( chunk.size() ) { *ret += chunk + suffix; return (*next)(); } return *ret; }); }); return (*next)(); }); }

.then() . async/await:

task<std::string> read( std::string file, std::string suffix ) __async { std::istream fi = __await open(file); std::string ret, chunk; while( (chunk = __await fi.read()).size() ) ret += chunk + suffix; return ret; }

task<std::string> , . await , .then(). __async __await , Visual Studio.

. , - . std::future, auto .then , - , . resumable . , , , async/await. resumable , "" , std::future, 1:0 resumable .

. , resumable . , ++? . . . - 2 : . - ? . , . . ? ? futures (- ). , , , . resumable (, ) " ": , , , - " ".

, resumable . std::(shared_)future void. , boost::future hpx::future. , "", . - resumable VArgs, -. , variadic templates. , , resumable , , std::future - copy/move .


, ++14. -, ( ?) ++14 ( ) - . - ++11 . , - ++y1. resumable - (TS), - WG21. , resumable , . , . , CTP Visual Studio ( . : !). __async __await.

, . ++14 .then() future, await std::future::get.


. - , - . resumable . , Cilk ( - ). - resumable - - , .

( __async\__await ?). resumable STL Boost - .

, resumable - - (. ). , , . -, ? , , , - .

, , resumable . - resumable lambda functions ? - WG21, 2013 .

- N3722
resumable . , async resumable. , resumable "resumable", - . await .

, std::future . , s, :

get(), then(), callable object s, s& const s. get().

is_ready(), future


, s::promise_type, resumable . set_value(T) set_exception(exception_ptr). s::promise_type s.


, " " "". STL , , , - . - sequence , , , ( ).

yield:

sequence<int> range(int low, int high) resumable { for(int i = low; i <= high; ++i) { yield i; } }


yield i sequence. sequence , yield. - , , . yield await .
 std::future. 

resumable . await "" future, . await (, , "!").

. await - std::future, . , Hartmut Kaiser - :

std::future<uint64_t> fibonacci(uint64_t n) async { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs = std::async(&fibonacci, n-1); std::future<uint64_t> rhs = fibonacci(n-2); return await lhs + await rhs; }


resumable . lhs std::future , await, std::future .

, resumable - , await future . , , await. , await. , , .


resumable , " ?". - . , . resumable . Thomas Heller ( ) resumable .

std::future< uint64_t> fibonacci(uint64_t n) { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs_future = std::async(&fibonacci, n-1); //.unwrap(); std::future<uint64_t> rhs_future = fibonacci(n-2); return dataflow( unwrapped([](uint64_t lhs, uint64_t rhs) { return lhs + rhs; }) , lhs_future, rhs_future ); }

. , "dataflow" await . future, . ++11 ++14 - .

- resumable ?
, , . Hartmut Kaiser , resumable . , \ . , .

resumable
- ( ) resumable . - , , . async/await, (if/else, for ..). . N3650 , std::future:

future<int> f(shared_ptr str) { shared_ptr<vector> buf = ...; return str->read(512, buf) .then([](future<int> op)// lambda 1 { return op.get() + 11; }); } future<void> g() { shared_ptr s = ...; return f(s).then([s](future<int> op) // lambda 2 { s->close(); }); }

resumable :

future<void> f(stream str) async { shared_ptr<vector> buf = ...; int count = await str.read(512, buf); return count + 11; } future g() async { stream s = ...; int pls11 = await f(s); s.close(); }

resumable (, , ). . , Herb Sutter BUILD:

std::string read( std::string file, std::string suffix ) { std::istream fi = open(file).get(); std::string ret, chunk; while( (chunk = fi.read().get()).size() ) ret += chunk + suffix; return ret; }

" " - future::get() std::future. , get() then(). , .

task<std::string> read( std::string file, std::string suffix ) { return open(file) .then([=](std::istream fi) { auto ret = std::make_shared<std::string>(); auto next = std::make_shared<std::function<task()>>( [=]{ fi.read() .then([=](std::string chunk) { if( chunk.size() ) { *ret += chunk + suffix; return (*next)(); } return *ret; }); }); return (*next)(); }); }

.then() . async/await:

task<std::string> read( std::string file, std::string suffix ) __async { std::istream fi = __await open(file); std::string ret, chunk; while( (chunk = __await fi.read()).size() ) ret += chunk + suffix; return ret; }

task<std::string> , . await , .then(). __async __await , Visual Studio.

. , - . std::future, auto .then , - , . resumable . , , , async/await. resumable , "" , std::future, 1:0 resumable .

. , resumable . , ++? . . . - 2 : . - ? . , . . ? ? futures (- ). , , , . resumable (, ) " ": , , , - " ".

, resumable . std::(shared_)future void. , boost::future hpx::future. , "", . - resumable VArgs, -. , variadic templates. , , resumable , , std::future - copy/move .


, ++14. -, ( ?) ++14 ( ) - . - ++11 . , - ++y1. resumable - (TS), - WG21. , resumable , . , . , CTP Visual Studio ( . : !). __async __await.

, . ++14 .then() future, await std::future::get.


. - , - . resumable . , Cilk ( - ). - resumable - - , .

( __async\__await ?). resumable STL Boost - .

, resumable - - (. ). , , . -, ? , , , - .

, , resumable . - resumable lambda functions ? - WG21, 2013 .

- N3722
resumable . , async resumable. , resumable "resumable", - . await .

, std::future . , s, :

get(), then(), callable object s, s& const s. get().

is_ready(), future


, s::promise_type, resumable . set_value(T) set_exception(exception_ptr). s::promise_type s.


, " " "". STL , , , - . - sequence , , , ( ).

yield:

sequence<int> range(int low, int high) resumable { for(int i = low; i <= high; ++i) { yield i; } }


yield i sequence. sequence , yield. - , , . yield await .
std::future.

resumable . await "" future, . await (, , "!").

. await - std::future, . , Hartmut Kaiser - :

std::future<uint64_t> fibonacci(uint64_t n) async { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs = std::async(&fibonacci, n-1); std::future<uint64_t> rhs = fibonacci(n-2); return await lhs + await rhs; }


resumable . lhs std::future , await, std::future .

, resumable - , await future . , , await. , await. , , .


resumable , " ?". - . , . resumable . Thomas Heller ( ) resumable .

std::future< uint64_t> fibonacci(uint64_t n) { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs_future = std::async(&fibonacci, n-1); //.unwrap(); std::future<uint64_t> rhs_future = fibonacci(n-2); return dataflow( unwrapped([](uint64_t lhs, uint64_t rhs) { return lhs + rhs; }) , lhs_future, rhs_future ); }

. , "dataflow" await . future, . ++11 ++14 - .

- resumable ?
, , . Hartmut Kaiser , resumable . , \ . , .

resumable
- ( ) resumable . - , , . async/await, (if/else, for ..). . N3650 , std::future:

future<int> f(shared_ptr str) { shared_ptr<vector> buf = ...; return str->read(512, buf) .then([](future<int> op)// lambda 1 { return op.get() + 11; }); } future<void> g() { shared_ptr s = ...; return f(s).then([s](future<int> op) // lambda 2 { s->close(); }); }

resumable :

future<void> f(stream str) async { shared_ptr<vector> buf = ...; int count = await str.read(512, buf); return count + 11; } future g() async { stream s = ...; int pls11 = await f(s); s.close(); }

resumable (, , ). . , Herb Sutter BUILD:

std::string read( std::string file, std::string suffix ) { std::istream fi = open(file).get(); std::string ret, chunk; while( (chunk = fi.read().get()).size() ) ret += chunk + suffix; return ret; }

" " - future::get() std::future. , get() then(). , .

task<std::string> read( std::string file, std::string suffix ) { return open(file) .then([=](std::istream fi) { auto ret = std::make_shared<std::string>(); auto next = std::make_shared<std::function<task()>>( [=]{ fi.read() .then([=](std::string chunk) { if( chunk.size() ) { *ret += chunk + suffix; return (*next)(); } return *ret; }); }); return (*next)(); }); }

.then() . async/await:

task<std::string> read( std::string file, std::string suffix ) __async { std::istream fi = __await open(file); std::string ret, chunk; while( (chunk = __await fi.read()).size() ) ret += chunk + suffix; return ret; }

task<std::string> , . await , .then(). __async __await , Visual Studio.

. , - . std::future, auto .then , - , . resumable . , , , async/await. resumable , "" , std::future, 1:0 resumable .

. , resumable . , ++? . . . - 2 : . - ? . , . . ? ? futures (- ). , , , . resumable (, ) " ": , , , - " ".

, resumable . std::(shared_)future void. , boost::future hpx::future. , "", . - resumable VArgs, -. , variadic templates. , , resumable , , std::future - copy/move .


, ++14. -, ( ?) ++14 ( ) - . - ++11 . , - ++y1. resumable - (TS), - WG21. , resumable , . , . , CTP Visual Studio ( . : !). __async __await.

, . ++14 .then() future, await std::future::get.


. - , - . resumable . , Cilk ( - ). - resumable - - , .

( __async\__await ?). resumable STL Boost - .

, resumable - - (. ). , , . -, ? , , , - .

, , resumable . - resumable lambda functions ? - WG21, 2013 .

- N3722
resumable . , async resumable. , resumable "resumable", - . await .

, std::future . , s, :

get(), then(), callable object s, s& const s. get().

is_ready(), future


, s::promise_type, resumable . set_value(T) set_exception(exception_ptr). s::promise_type s.


, " " "". STL , , , - . - sequence , , , ( ).

yield:

sequence<int> range(int low, int high) resumable { for(int i = low; i <= high; ++i) { yield i; } }


yield i sequence. sequence , yield. - , , . yield await .
std::future.

resumable . await "" future, . await (, , "!").

. await - std::future, . , Hartmut Kaiser - :

std::future<uint64_t> fibonacci(uint64_t n) async { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs = std::async(&fibonacci, n-1); std::future<uint64_t> rhs = fibonacci(n-2); return await lhs + await rhs; }


resumable . lhs std::future , await, std::future .

, resumable - , await future . , , await. , await. , , .


resumable , " ?". - . , . resumable . Thomas Heller ( ) resumable .

std::future< uint64_t> fibonacci(uint64_t n) { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs_future = std::async(&fibonacci, n-1); //.unwrap(); std::future<uint64_t> rhs_future = fibonacci(n-2); return dataflow( unwrapped([](uint64_t lhs, uint64_t rhs) { return lhs + rhs; }) , lhs_future, rhs_future ); }

. , "dataflow" await . future, . ++11 ++14 - .

- resumable ?
, , . Hartmut Kaiser , resumable . , \ . , .

resumable
- ( ) resumable . - , , . async/await, (if/else, for ..). . N3650 , std::future:

future<int> f(shared_ptr str) { shared_ptr<vector> buf = ...; return str->read(512, buf) .then([](future<int> op)// lambda 1 { return op.get() + 11; }); } future<void> g() { shared_ptr s = ...; return f(s).then([s](future<int> op) // lambda 2 { s->close(); }); }

resumable :

future<void> f(stream str) async { shared_ptr<vector> buf = ...; int count = await str.read(512, buf); return count + 11; } future g() async { stream s = ...; int pls11 = await f(s); s.close(); }

resumable (, , ). . , Herb Sutter BUILD:

std::string read( std::string file, std::string suffix ) { std::istream fi = open(file).get(); std::string ret, chunk; while( (chunk = fi.read().get()).size() ) ret += chunk + suffix; return ret; }

" " - future::get() std::future. , get() then(). , .

task<std::string> read( std::string file, std::string suffix ) { return open(file) .then([=](std::istream fi) { auto ret = std::make_shared<std::string>(); auto next = std::make_shared<std::function<task()>>( [=]{ fi.read() .then([=](std::string chunk) { if( chunk.size() ) { *ret += chunk + suffix; return (*next)(); } return *ret; }); }); return (*next)(); }); }

.then() . async/await:

task<std::string> read( std::string file, std::string suffix ) __async { std::istream fi = __await open(file); std::string ret, chunk; while( (chunk = __await fi.read()).size() ) ret += chunk + suffix; return ret; }

task<std::string> , . await , .then(). __async __await , Visual Studio.

. , - . std::future, auto .then , - , . resumable . , , , async/await. resumable , "" , std::future, 1:0 resumable .

. , resumable . , ++? . . . - 2 : . - ? . , . . ? ? futures (- ). , , , . resumable (, ) " ": , , , - " ".

, resumable . std::(shared_)future void. , boost::future hpx::future. , "", . - resumable VArgs, -. , variadic templates. , , resumable , , std::future - copy/move .


, ++14. -, ( ?) ++14 ( ) - . - ++11 . , - ++y1. resumable - (TS), - WG21. , resumable , . , . , CTP Visual Studio ( . : !). __async __await.

, . ++14 .then() future, await std::future::get.


. - , - . resumable . , Cilk ( - ). - resumable - - , .

( __async\__await ?). resumable STL Boost - .

, resumable - - (. ). , , . -, ? , , , - .

, , resumable . - resumable lambda functions ? - WG21, 2013 .

- N3722
resumable . , async resumable. , resumable "resumable", - . await .

, std::future . , s, :

get(), then(), callable object s, s& const s. get().
is_ready(), future


, s::promise_type, resumable . set_value(T) set_exception(exception_ptr). s::promise_type s.


, " " "". STL , , , - . - sequence , , , ( ).

yield:

sequence<int> range(int low, int high) resumable { for(int i = low; i <= high; ++i) { yield i; } }


yield i sequence. sequence , yield. - , , . yield await .
std::future.

resumable . await "" future, . await (, , "!").

. await - std::future, . , Hartmut Kaiser - :

std::future<uint64_t> fibonacci(uint64_t n) async { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs = std::async(&fibonacci, n-1); std::future<uint64_t> rhs = fibonacci(n-2); return await lhs + await rhs; }


resumable . lhs std::future , await, std::future .

, resumable - , await future . , , await. , await. , , .


resumable , " ?". - . , . resumable . Thomas Heller ( ) resumable .

std::future< uint64_t> fibonacci(uint64_t n) { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs_future = std::async(&fibonacci, n-1); //.unwrap(); std::future<uint64_t> rhs_future = fibonacci(n-2); return dataflow( unwrapped([](uint64_t lhs, uint64_t rhs) { return lhs + rhs; }) , lhs_future, rhs_future ); }

. , "dataflow" await . future, . ++11 ++14 - .

- resumable ?
, , . Hartmut Kaiser , resumable . , \ . , .

resumable
- ( ) resumable . - , , . async/await, (if/else, for ..). . N3650 , std::future:

future<int> f(shared_ptr str) { shared_ptr<vector> buf = ...; return str->read(512, buf) .then([](future<int> op)// lambda 1 { return op.get() + 11; }); } future<void> g() { shared_ptr s = ...; return f(s).then([s](future<int> op) // lambda 2 { s->close(); }); }

resumable :

future<void> f(stream str) async { shared_ptr<vector> buf = ...; int count = await str.read(512, buf); return count + 11; } future g() async { stream s = ...; int pls11 = await f(s); s.close(); }

resumable (, , ). . , Herb Sutter BUILD:

std::string read( std::string file, std::string suffix ) { std::istream fi = open(file).get(); std::string ret, chunk; while( (chunk = fi.read().get()).size() ) ret += chunk + suffix; return ret; }

" " - future::get() std::future. , get() then(). , .

task<std::string> read( std::string file, std::string suffix ) { return open(file) .then([=](std::istream fi) { auto ret = std::make_shared<std::string>(); auto next = std::make_shared<std::function<task()>>( [=]{ fi.read() .then([=](std::string chunk) { if( chunk.size() ) { *ret += chunk + suffix; return (*next)(); } return *ret; }); }); return (*next)(); }); }

.then() . async/await:

task<std::string> read( std::string file, std::string suffix ) __async { std::istream fi = __await open(file); std::string ret, chunk; while( (chunk = __await fi.read()).size() ) ret += chunk + suffix; return ret; }

task<std::string> , . await , .then(). __async __await , Visual Studio.

. , - . std::future, auto .then , - , . resumable . , , , async/await. resumable , "" , std::future, 1:0 resumable .

. , resumable . , ++? . . . - 2 : . - ? . , . . ? ? futures (- ). , , , . resumable (, ) " ": , , , - " ".

, resumable . std::(shared_)future void. , boost::future hpx::future. , "", . - resumable VArgs, -. , variadic templates. , , resumable , , std::future - copy/move .


, ++14. -, ( ?) ++14 ( ) - . - ++11 . , - ++y1. resumable - (TS), - WG21. , resumable , . , . , CTP Visual Studio ( . : !). __async __await.

, . ++14 .then() future, await std::future::get.


. - , - . resumable . , Cilk ( - ). - resumable - - , .

( __async\__await ?). resumable STL Boost - .

, resumable - - (. ). , , . -, ? , , , - .

, , resumable . - resumable lambda functions ? - WG21, 2013 .

- N3722
resumable . , async resumable. , resumable "resumable", - . await .

, std::future . , s, :

get(), then(), callable object s, s& const s. get().
is_ready(), future


, s::promise_type, resumable . set_value(T) set_exception(exception_ptr). s::promise_type s.


, " " "". STL , , , - . - sequence , , , ( ).

yield:

sequence<int> range(int low, int high) resumable { for(int i = low; i <= high; ++i) { yield i; } }


yield i sequence. sequence , yield. - , , . yield await .
std::future.

resumable . await "" future, . await (, , "!").

. await - std::future, . , Hartmut Kaiser - :

std::future<uint64_t> fibonacci(uint64_t n) async { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs = std::async(&fibonacci, n-1); std::future<uint64_t> rhs = fibonacci(n-2); return await lhs + await rhs; }


resumable . lhs std::future , await, std::future .

, resumable - , await future . , , await. , await. , , .


resumable , " ?". - . , . resumable . Thomas Heller ( ) resumable .

std::future< uint64_t> fibonacci(uint64_t n) { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs_future = std::async(&fibonacci, n-1); //.unwrap(); std::future<uint64_t> rhs_future = fibonacci(n-2); return dataflow( unwrapped([](uint64_t lhs, uint64_t rhs) { return lhs + rhs; }) , lhs_future, rhs_future ); }

. , "dataflow" await . future, . ++11 ++14 - .

- resumable ?
, , . Hartmut Kaiser , resumable . , \ . , .

resumable
- ( ) resumable . - , , . async/await, (if/else, for ..). . N3650 , std::future:

future<int> f(shared_ptr str) { shared_ptr<vector> buf = ...; return str->read(512, buf) .then([](future<int> op)// lambda 1 { return op.get() + 11; }); } future<void> g() { shared_ptr s = ...; return f(s).then([s](future<int> op) // lambda 2 { s->close(); }); }

resumable :

future<void> f(stream str) async { shared_ptr<vector> buf = ...; int count = await str.read(512, buf); return count + 11; } future g() async { stream s = ...; int pls11 = await f(s); s.close(); }

resumable (, , ). . , Herb Sutter BUILD:

std::string read( std::string file, std::string suffix ) { std::istream fi = open(file).get(); std::string ret, chunk; while( (chunk = fi.read().get()).size() ) ret += chunk + suffix; return ret; }

" " - future::get() std::future. , get() then(). , .

task<std::string> read( std::string file, std::string suffix ) { return open(file) .then([=](std::istream fi) { auto ret = std::make_shared<std::string>(); auto next = std::make_shared<std::function<task()>>( [=]{ fi.read() .then([=](std::string chunk) { if( chunk.size() ) { *ret += chunk + suffix; return (*next)(); } return *ret; }); }); return (*next)(); }); }

.then() . async/await:

task<std::string> read( std::string file, std::string suffix ) __async { std::istream fi = __await open(file); std::string ret, chunk; while( (chunk = __await fi.read()).size() ) ret += chunk + suffix; return ret; }

task<std::string> , . await , .then(). __async __await , Visual Studio.

. , - . std::future, auto .then , - , . resumable . , , , async/await. resumable , "" , std::future, 1:0 resumable .

. , resumable . , ++? . . . - 2 : . - ? . , . . ? ? futures (- ). , , , . resumable (, ) " ": , , , - " ".

, resumable . std::(shared_)future void. , boost::future hpx::future. , "", . - resumable VArgs, -. , variadic templates. , , resumable , , std::future - copy/move .


, ++14. -, ( ?) ++14 ( ) - . - ++11 . , - ++y1. resumable - (TS), - WG21. , resumable , . , . , CTP Visual Studio ( . : !). __async __await.

, . ++14 .then() future, await std::future::get.


. - , - . resumable . , Cilk ( - ). - resumable - - , .

( __async\__await ?). resumable STL Boost - .

, resumable - - (. ). , , . -, ? , , , - .

, , resumable . - resumable lambda functions ? - WG21, 2013 .

- N3722
resumable . , async resumable. , resumable "resumable", - . await .

, std::future . , s, :

get(), then(), callable object s, s& const s. get().
is_ready(), future


, s::promise_type, resumable . set_value(T) set_exception(exception_ptr). s::promise_type s.


, " " "". STL , , , - . - sequence , , , ( ).

yield:

sequence<int> range(int low, int high) resumable { for(int i = low; i <= high; ++i) { yield i; } }


yield i sequence. sequence , yield. - , , . yield await .
std::future.

resumable . await "" future, . await (, , "!").

. await - std::future, . , Hartmut Kaiser - :

std::future<uint64_t> fibonacci(uint64_t n) async { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs = std::async(&fibonacci, n-1); std::future<uint64_t> rhs = fibonacci(n-2); return await lhs + await rhs; }


resumable . lhs std::future , await, std::future .

, resumable - , await future . , , await. , await. , , .


resumable , " ?". - . , . resumable . Thomas Heller ( ) resumable .

std::future< uint64_t> fibonacci(uint64_t n) { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs_future = std::async(&fibonacci, n-1); //.unwrap(); std::future<uint64_t> rhs_future = fibonacci(n-2); return dataflow( unwrapped([](uint64_t lhs, uint64_t rhs) { return lhs + rhs; }) , lhs_future, rhs_future ); }

. , "dataflow" await . future, . ++11 ++14 - .

- resumable ?
, , . Hartmut Kaiser , resumable . , \ . , .

resumable
- ( ) resumable . - , , . async/await, (if/else, for ..). . N3650 , std::future:

future<int> f(shared_ptr str) { shared_ptr<vector> buf = ...; return str->read(512, buf) .then([](future<int> op)// lambda 1 { return op.get() + 11; }); } future<void> g() { shared_ptr s = ...; return f(s).then([s](future<int> op) // lambda 2 { s->close(); }); }

resumable :

future<void> f(stream str) async { shared_ptr<vector> buf = ...; int count = await str.read(512, buf); return count + 11; } future g() async { stream s = ...; int pls11 = await f(s); s.close(); }

resumable (, , ). . , Herb Sutter BUILD:

std::string read( std::string file, std::string suffix ) { std::istream fi = open(file).get(); std::string ret, chunk; while( (chunk = fi.read().get()).size() ) ret += chunk + suffix; return ret; }

" " - future::get() std::future. , get() then(). , .

task<std::string> read( std::string file, std::string suffix ) { return open(file) .then([=](std::istream fi) { auto ret = std::make_shared<std::string>(); auto next = std::make_shared<std::function<task()>>( [=]{ fi.read() .then([=](std::string chunk) { if( chunk.size() ) { *ret += chunk + suffix; return (*next)(); } return *ret; }); }); return (*next)(); }); }

.then() . async/await:

task<std::string> read( std::string file, std::string suffix ) __async { std::istream fi = __await open(file); std::string ret, chunk; while( (chunk = __await fi.read()).size() ) ret += chunk + suffix; return ret; }

task<std::string> , . await , .then(). __async __await , Visual Studio.

. , - . std::future, auto .then , - , . resumable . , , , async/await. resumable , "" , std::future, 1:0 resumable .

. , resumable . , ++? . . . - 2 : . - ? . , . . ? ? futures (- ). , , , . resumable (, ) " ": , , , - " ".

, resumable . std::(shared_)future void. , boost::future hpx::future. , "", . - resumable VArgs, -. , variadic templates. , , resumable , , std::future - copy/move .


, ++14. -, ( ?) ++14 ( ) - . - ++11 . , - ++y1. resumable - (TS), - WG21. , resumable , . , . , CTP Visual Studio ( . : !). __async __await.

, . ++14 .then() future, await std::future::get.


. - , - . resumable . , Cilk ( - ). - resumable - - , .

( __async\__await ?). resumable STL Boost - .

, resumable - - (. ). , , . -, ? , , , - .

, , resumable . - resumable lambda functions ? - WG21, 2013 .

- N3722
resumable . , async resumable. , resumable "resumable", - . await .

, std::future . , s, :

get(), then(), callable object s, s& const s. get().
is_ready(), future


, s::promise_type, resumable . set_value(T) set_exception(exception_ptr). s::promise_type s.


, " " "". STL , , , - . - sequence , , , ( ).

yield:

sequence<int> range(int low, int high) resumable { for(int i = low; i <= high; ++i) { yield i; } }


yield i sequence. sequence , yield. - , , . yield await .
std::future.

resumable . await "" future, . await (, , "!").

. await - std::future, . , Hartmut Kaiser - :

std::future<uint64_t> fibonacci(uint64_t n) async { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs = std::async(&fibonacci, n-1); std::future<uint64_t> rhs = fibonacci(n-2); return await lhs + await rhs; }


resumable . lhs std::future , await, std::future .

, resumable - , await future . , , await. , await. , , .


resumable , " ?". - . , . resumable . Thomas Heller ( ) resumable .

std::future< uint64_t> fibonacci(uint64_t n) { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs_future = std::async(&fibonacci, n-1); //.unwrap(); std::future<uint64_t> rhs_future = fibonacci(n-2); return dataflow( unwrapped([](uint64_t lhs, uint64_t rhs) { return lhs + rhs; }) , lhs_future, rhs_future ); }

. , "dataflow" await . future, . ++11 ++14 - .

- resumable ?
, , . Hartmut Kaiser , resumable . , \ . , .

resumable
- ( ) resumable . - , , . async/await, (if/else, for ..). . N3650 , std::future:

future<int> f(shared_ptr str) { shared_ptr<vector> buf = ...; return str->read(512, buf) .then([](future<int> op)// lambda 1 { return op.get() + 11; }); } future<void> g() { shared_ptr s = ...; return f(s).then([s](future<int> op) // lambda 2 { s->close(); }); }

resumable :

future<void> f(stream str) async { shared_ptr<vector> buf = ...; int count = await str.read(512, buf); return count + 11; } future g() async { stream s = ...; int pls11 = await f(s); s.close(); }

resumable (, , ). . , Herb Sutter BUILD:

std::string read( std::string file, std::string suffix ) { std::istream fi = open(file).get(); std::string ret, chunk; while( (chunk = fi.read().get()).size() ) ret += chunk + suffix; return ret; }

" " - future::get() std::future. , get() then(). , .

task<std::string> read( std::string file, std::string suffix ) { return open(file) .then([=](std::istream fi) { auto ret = std::make_shared<std::string>(); auto next = std::make_shared<std::function<task()>>( [=]{ fi.read() .then([=](std::string chunk) { if( chunk.size() ) { *ret += chunk + suffix; return (*next)(); } return *ret; }); }); return (*next)(); }); }

.then() . async/await:

task<std::string> read( std::string file, std::string suffix ) __async { std::istream fi = __await open(file); std::string ret, chunk; while( (chunk = __await fi.read()).size() ) ret += chunk + suffix; return ret; }

task<std::string> , . await , .then(). __async __await , Visual Studio.

. , - . std::future, auto .then , - , . resumable . , , , async/await. resumable , "" , std::future, 1:0 resumable .

. , resumable . , ++? . . . - 2 : . - ? . , . . ? ? futures (- ). , , , . resumable (, ) " ": , , , - " ".

, resumable . std::(shared_)future void. , boost::future hpx::future. , "", . - resumable VArgs, -. , variadic templates. , , resumable , , std::future - copy/move .


, ++14. -, ( ?) ++14 ( ) - . - ++11 . , - ++y1. resumable - (TS), - WG21. , resumable , . , . , CTP Visual Studio ( . : !). __async __await.

, . ++14 .then() future, await std::future::get.


. - , - . resumable . , Cilk ( - ). - resumable - - , .

( __async\__await ?). resumable STL Boost - .

, resumable - - (. ). , , . -, ? , , , - .

, , resumable . - resumable lambda functions ? - WG21, 2013 .

- N3722
resumable . , async resumable. , resumable "resumable", - . await .

, std::future . , s, :

get(), then(), callable object s, s& const s. get().
is_ready(), future


, s::promise_type, resumable . set_value(T) set_exception(exception_ptr). s::promise_type s.


, " " "". STL , , , - . - sequence , , , ( ).

yield:

sequence<int> range(int low, int high) resumable { for(int i = low; i <= high; ++i) { yield i; } }


yield i sequence. sequence , yield. - , , . yield await .
std::future.

resumable . await "" future, . await (, , "!").

. await - std::future, . , Hartmut Kaiser - :

std::future<uint64_t> fibonacci(uint64_t n) async { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs = std::async(&fibonacci, n-1); std::future<uint64_t> rhs = fibonacci(n-2); return await lhs + await rhs; }


resumable . lhs std::future , await, std::future .

, resumable - , await future . , , await. , await. , , .


resumable , " ?". - . , . resumable . Thomas Heller ( ) resumable .

std::future< uint64_t> fibonacci(uint64_t n) { if (n < 2) return std::make_ready_future(n); std::future<uint64_t> lhs_future = std::async(&fibonacci, n-1); //.unwrap(); std::future<uint64_t> rhs_future = fibonacci(n-2); return dataflow( unwrapped([](uint64_t lhs, uint64_t rhs) { return lhs + rhs; }) , lhs_future, rhs_future ); }

. , "dataflow" await . future, . ++11 ++14 - .

- resumable ?
, , . Hartmut Kaiser , resumable . , \ . , .

resumable
- ( ) resumable . - , , . async/await, (if/else, for ..). . N3650 , std::future:

future<int> f(shared_ptr str) { shared_ptr<vector> buf = ...; return str->read(512, buf) .then([](future<int> op)// lambda 1 { return op.get() + 11; }); } future<void> g() { shared_ptr s = ...; return f(s).then([s](future<int> op) // lambda 2 { s->close(); }); }

resumable :

future<void> f(stream str) async { shared_ptr<vector> buf = ...; int count = await str.read(512, buf); return count + 11; } future g() async { stream s = ...; int pls11 = await f(s); s.close(); }

resumable (, , ). . , Herb Sutter BUILD:

std::string read( std::string file, std::string suffix ) { std::istream fi = open(file).get(); std::string ret, chunk; while( (chunk = fi.read().get()).size() ) ret += chunk + suffix; return ret; }

" " - future::get() std::future. , get() then(). , .

task<std::string> read( std::string file, std::string suffix ) { return open(file) .then([=](std::istream fi) { auto ret = std::make_shared<std::string>(); auto next = std::make_shared<std::function<task()>>( [=]{ fi.read() .then([=](std::string chunk) { if( chunk.size() ) { *ret += chunk + suffix; return (*next)(); } return *ret; }); }); return (*next)(); }); }

.then() . async/await:

task<std::string> read( std::string file, std::string suffix ) __async { std::istream fi = __await open(file); std::string ret, chunk; while( (chunk = __await fi.read()).size() ) ret += chunk + suffix; return ret; }

task<std::string> , . await , .then(). __async __await , Visual Studio.

. , - . std::future, auto .then , - , . resumable . , , , async/await. resumable , "" , std::future, 1:0 resumable .

. , resumable . , ++? . . . - 2 : . - ? . , . . ? ? futures (- ). , , , . resumable (, ) " ": , , , - " ".

, resumable . std::(shared_)future void. , boost::future hpx::future. , "", . - resumable VArgs, -. , variadic templates. , , resumable , , std::future - copy/move .


, ++14. -, ( ?) ++14 ( ) - . - ++11 . , - ++y1. resumable - (TS), - WG21. , resumable , . , . , CTP Visual Studio ( . : !). __async __await.

, . ++14 .then() future, await std::future::get.


. - , - . resumable . , Cilk ( - ). - resumable - - , .

( __async\__await ?). resumable STL Boost - .

, resumable - - (. ). , , . -, ? , , , - .

, , resumable . - resumable lambda functions ? - WG21, 2013 .

- N3722
resumable . , async resumable. , resumable "resumable", - . await .

, std::future . , s, :

get(), then(), callable object s, s& const s. get().
is_ready(), future


, s::promise_type, resumable . set_value(T) set_exception(exception_ptr). s::promise_type s.


, " " "". STL , , , - . - sequence , , , ( ).

yield:

sequence<int> range(int low, int high) resumable { for(int i = low; i <= high; ++i) { yield i; } }


yield i sequence. sequence , yield. - , , . yield await .

Source: https://habr.com/ru/post/203724/


All Articles