Presentation is loading. Please wait.

Presentation is loading. Please wait.

3 string read_string_from_file(string file); string s = read_string_from_file("myfile.txt"); cout << s;

Similar presentations


Presentation on theme: "3 string read_string_from_file(string file); string s = read_string_from_file("myfile.txt"); cout << s;"— Presentation transcript:

1

2

3 3

4 string read_string_from_file(string file); string s = read_string_from_file("myfile.txt"); cout << s;

5 template void read_string_from_file(string file, Func&& func);... read_string_from_file("myfile.txt", [](string s) { cout << s; });

6 template void concatenate_files(string file1, string file2, Func&& func) { read_string_from_file(file1, [func](string str1) { read_string_from_file(file2, [func](string str2) { func(str1 + str2); }); }

7 template void concatenate_files(string file1, string file2, Func&& func) { auto results = make_shared (); read_string_from_file(file1, [=](string str) { if (results->str) { func(str + *results->str); } else{ results->str = make_unique (str); } }); read_string_from_file(file2, [=](string str) { if (results->str) { func(*results->str + str); } else{ results->str = make_unique (str); } }); } struct result_holder { unique_ptr str; }; ? Spot the defect!

8

9 // Launch a task: future work = async([] { return CountLinesInFile(…); }); // Collect the results: cout << work.get();

10 // Launch a task: future work1 = async([] { return CountLinesInFile(…); }); // Launch another task: future work2 = async([] { return CountLinesInFile(…); }); // Collect the results: cout << work1.get() + work2.get();

11 // Launch a task: future work = CountLinesInFileAsync(...); work.then([] (future f) { // Get the result cout << f.get(); });

12 future t = async([]() { return func1(); }).then ([](future n) { return func2(n.get()); }).then ([](future n) { return func3(n.get()); }).then...

13 f.then(A).then(B); f.then(A); f.then(B); auto f = when_all(a, b); auto f = when_any(a, b);

14 vector > futures = get_futures(); auto futureResult = when_all (begin(futures), end(futures)).then([](future >> results) { for (auto& s : results.get() ) // will not block { cout << s.get(); // will not block } });

15 future future1 =...; future future2 =...; auto futureResult = when_all(future1, future2).then([](future, future >> results) { auto pair = results.get(); // will not block... } });

16 future concatenate_files(string file1, string file2) { auto strings = when_all(read_string_from_file(file1), read_string_from_file(file2)); return strings.then([]( future, future >> strings) { auto pair = strings.get(); return pair.get.get() + pair.get.get(); }); }

17 future concatenate_files(string file1, string file2) { auto strings = when_all(read_string_from_file(file1), read_string_from_file(file2)); return strings.then([]( auto strings) { auto pair = strings.get(); return pair.get.get() + pair.get.get(); }); }

18 vector > futures = get_futures(); auto futureResult = when_any (begin(futures), end(futures)).then([](future >> results) { for (auto& s : results.get() ) // will not block { if(s.ready()) cout << s.get(); // will not block } });

19 future compute(int x) { if (x (-1); if (x == 0) return make_ready_future (0); future f1 = async([]() { return do_work(x); }); return f1; }

20 future f1 = async([]() { return possibly_long_computation(); }); if(!f1.is_ready()) { //if not ready, attach a continuation and avoid a blocking wait fl.then([] (future f2) { int v = f2.get(); process_value(v); }); } // if ready, no need to add continuation, process value right away else { int v = f1.get(); process_value(v); }

21

22 22.get.then string read(string file) { istream fi = open(file).get(); string ret, chunk; while ((chunk = fi.read().get()).size()) ret += chunk; return ret; } future read(string file) { return open(file).then([=](istream fi) { string ret, chunk; while ( ???

23 23.get.then string read(string file) { istream fi = open(file).get(); string ret, chunk; while ((chunk = fi.read().get()).size()) ret += chunk; return ret; } future read(string file) { return open(file).then([=](istream fi) { auto ret = make_shared (); auto next = make_shared ()>>( [=] { fi.read().then([=](string chunk) { if (chunk.size()) { *ret += chunk; return next(); } return make_ready_future(*ret); }); return (*next)(); }); }

24

25 future f() resumable { future f1 = async([]() { return possibly_long_computation(); }); int n = await f1; return to_string(n); }

26 26.get await string read(string file) { istream fi = open(file).get(); string ret, chunk; while ((chunk = fi.read().get()).size()) ret += chunk; return ret; } String read(string file) { istream fi = await open(file); string ret, chunk; while ((chunk = (await fi.read()).size()) ret += chunk; return ret; }

27 Thread #2Thread #1 27 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!

28

29

30


Download ppt "3 string read_string_from_file(string file); string s = read_string_from_file("myfile.txt"); cout << s;"

Similar presentations


Ads by Google