Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ await Deon Brewis std::future additions: isocpp.org/files/papers/N3721.pdf resumable functions: isocpp.org/files/papers/N3722.pdf.

Similar presentations


Presentation on theme: "C++ await Deon Brewis std::future additions: isocpp.org/files/papers/N3721.pdf resumable functions: isocpp.org/files/papers/N3722.pdf."— Presentation transcript:

1 C++ await Deon Brewis std::future additions: isocpp.org/files/papers/N3721.pdf resumable functions: isocpp.org/files/papers/N3722.pdf

2 PPL vs. std::future 2 PPLstd::future concurrency::task Calc( int x, int y ) resumable { int i = await concurrency::create_task( [=] { return x + y; } ); return i + 42; } Requires future.then – i.e. N3721 std::future Calc( int x, int y ) resumable { int i = await std::async( [=] { return x + y; } ); return i + 42; }

3 Motivation.get(),.then() and await comparison

4 Simple case.get size_t file_sizes( string file1, string file2 ) { task f1 = open(file1); task f2 = open(file2); return f1.get().size() + f2.get().size(); } 4

5 Simple case.get size_t file_sizes( string file1, string file2 ) { task f1 = open(file1); task f2 = open(file2); return f1.get().size() + f2.get().size(); }.then 5 task file_sizes( string file1, string file2 ) { task f1 = open(file1); task f2 = open(file2); return f1.then( [f2] (task tr1 ) { f2.then( [tr1] (task tr2) { return tr1.get().size() + tr2.get().size(); }) }); }

6 Simple case.get size_t file_sizes( string file1, string file2 ) { task f1 = open(file1); task f2 = open(file2); return f1.get().size() + f2.get().size(); }.then 6.then + await task file_sizes( string file1, string file2 ) resumable { task f1 = open(file1); task f2 = open(file2); return (await f1).size() + (await f2).size(); } task file_sizes( string file1, string file2 ) { task f1 = open(file1); task f2 = open(file2); return f1.then( [f2] (task tr1 ) { f2.then( [tr1] (task tr2) { return tr1.get().size() + tr2.get().size(); }) }); }

7 Branches and loops string read( string file, string suffix ) { istream fi = open(file).get(); string ret, chunk; while( (chunk = fi.read().get()).size() ) ret += chunk + suffix; return ret; } 7 ?.get.then

8 Branches and loops task read( string file, string suffix ) { return open(file).then([=](istream fi) { auto ret = make_shared (); auto next = make_shared ()>>( [=] { fi.read().then([=](string chunk) { if( chunk.size() ) { *ret += chunk + suffix; return next(); } return task_from_result(*ret); }); return (*next)(); }); } 8.get.then string read( string file, string suffix ) { istream fi = open(file).get(); string ret, chunk; while( (chunk = fi.read().get()).size() ) ret += chunk + suffix; return ret; }

9 Branches and loops task read( string file, string suffix ) { return open(file).then([=](istream fi) { auto ret = make_shared (); auto next = make_shared ()>>( [=] { fi.read().then([=](string chunk) { if( chunk.size() ) { *ret += chunk + suffix; return next(); } return task_from_result(*ret); }); return (*next)(); }); } 9 task read( string file, string suffix ) resumable { istream fi = await open(file); string ret, chunk; while( (chunk = await fi.read()).size() ) ret += chunk + suffix; return ret; }.get.then.then + await string read( string file, string suffix ) { istream fi = open(file).get(); string ret, chunk; while( (chunk = fi.read().get()).size() ) ret += chunk + suffix; return ret; }

10 Technical drill-down

11 Await implementations Iterator-Based Co-routines – Rewrite resumable function into a resumable state machine – Requires all locals to be hoisted to the heap Resumable Side Stacks – Do not rewrite function logic – Each resumable function execution has its own “side” stack – Side stack lives beyond suspension/resuming until logical completion 11 C++ C#

12 Thread #2 Thread #1 Resumable Side Stack simulation 12 Main Stack … … … … … foo(); t = async_bar() Side Stack async_bar(); do_work(); … … … await async_work(); somefunc(); bool b = t.get() More sync calls … return true; Side Stack async_work(); do_work(); await create_task(); Longrunning is done! return task return task return; void foo() { task t = async_bar(); somefunc(); bool b = t.get(); } task async_bar() resumable { do_work();... // sync calls await async_work(); // More sync calls... return true; } task async_work() resumable { do_work(); await create_task( [] { longrunning (); }); return; } done! async_work is done!

13 Demo


Download ppt "C++ await Deon Brewis std::future additions: isocpp.org/files/papers/N3721.pdf resumable functions: isocpp.org/files/papers/N3722.pdf."

Similar presentations


Ads by Google