Skip to content

Commit 1429d9e

Browse files
authored
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
1 parent 6e72adc commit 1429d9e

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

CppCoreGuidelines.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14963,8 +14963,30 @@ There is no explicit locking and both correct (value) return and error (exceptio
1496314963

1496414964
##### Example
1496514965

14966-
???
14966+
int read_value(const std::string& filename)
14967+
{
14968+
std::ifstream in(filename);
14969+
in.exceptions(std::ifstream::failbit);
14970+
int value;
14971+
in >> value;
14972+
return value;
14973+
}
14974+
1496714975

14976+
void async_example()
14977+
{
14978+
try
14979+
{
14980+
auto v1 = std::async(std::launch::async, read_value, "v1.txt");
14981+
auto v2 = std::async(std::launch::async, read_value, "v2.txt");
14982+
std::cout << v1.get() + v2.get() << '\n';
14983+
}
14984+
catch (std::ios_base::failure & fail)
14985+
{
14986+
// handle exception here
14987+
}
14988+
}
14989+
1496814990
##### Note
1496914991

1497014992
Unfortunately, `async()` is not perfect.

0 commit comments

Comments
 (0)