mirror of
https://github.com/DevEhChad/cpp-cheatsheet.git
synced 2025-11-08 14:01:34 +00:00
Add future
This commit is contained in:
17
README.md
17
README.md
@@ -554,3 +554,20 @@ thread t1(pingPongFn, sharedMes); // start example with 3 concurrent threads
|
|||||||
thread t2(pingPongFn, "pong");
|
thread t2(pingPongFn, "pong");
|
||||||
thread t3(pingPongFn, "boing");
|
thread t3(pingPongFn, "boing");
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## `future` (thread support library)
|
||||||
|
```cpp
|
||||||
|
#include <future> // Include future
|
||||||
|
function<int(int)> fib = // Create lambda function
|
||||||
|
[&](int i){
|
||||||
|
if (i <= 1){
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return fib(i-1)
|
||||||
|
+ fib(i-2);
|
||||||
|
};
|
||||||
|
future<int> fut = // result of async function
|
||||||
|
async(launch::async, fib, 4); // start async function in other thread
|
||||||
|
// do some other work
|
||||||
|
cout << fut.get(); // get result of async function. Wait if needed.
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user