From 1429d9ec719e869bea87ae98f94a89a96c122d7e Mon Sep 17 00:00:00 2001 From: roroberto <32388266+roroberto@users.noreply.github.com> Date: Thu, 30 Jan 2020 20:45:04 +0100 Subject: [PATCH] Added example to CP.61: Use an async() to spawn a concurrent task (#1557) Added example to CP.61: Use an async() to spawn a concurrent task --- CppCoreGuidelines.md | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 1260718..c447c83 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -14963,8 +14963,30 @@ There is no explicit locking and both correct (value) return and error (exceptio ##### Example - ??? + int read_value(const std::string& filename) + { + std::ifstream in(filename); + in.exceptions(std::ifstream::failbit); + int value; + in >> value; + return value; + } + + void async_example() + { + try + { + auto v1 = std::async(std::launch::async, read_value, "v1.txt"); + auto v2 = std::async(std::launch::async, read_value, "v2.txt"); + std::cout << v1.get() + v2.get() << '\n'; + } + catch (std::ios_base::failure & fail) + { + // handle exception here + } + } + ##### Note Unfortunately, `async()` is not perfect.