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
This commit is contained in:
roroberto
2020-01-30 20:45:04 +01:00
committed by GitHub
parent 6e72adc0fe
commit 1429d9ec71

View File

@@ -14963,7 +14963,29 @@ There is no explicit locking and both correct (value) return and error (exceptio
##### Example ##### 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 ##### Note