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
pull/1573/head
roroberto 6 years ago committed by GitHub
parent 6e72adc0fe
commit 1429d9ec71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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.

Loading…
Cancel
Save