Created
April 19, 2019 06:31
-
-
Save iamazeem/eb089682c0f92203d28d315304b523fe to your computer and use it in GitHub Desktop.
C++: std::async example (https://stackoverflow.com/a/51587626/7670262)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <future> | |
| #include <thread> | |
| #include <chrono> | |
| #define LOG() std::cout << __func__ << " : " | |
| void test() | |
| { | |
| LOG() << "IN\n"; | |
| using namespace std::chrono_literals; | |
| std::this_thread::sleep_for( 1s ); | |
| LOG() << "OUT\n"; | |
| } | |
| int main() | |
| { | |
| LOG() << "Calling test()...\n"; | |
| auto f = std::async( std::launch::async, test ); | |
| LOG() << "Running test()...\n"; | |
| // ... ... | |
| // ... You can do other stuff here ... | |
| // ... ... | |
| f.wait(); // Blocking call to wait for the result to be available | |
| LOG() << "Exiting...\n"; | |
| return 0; | |
| } | |
| /* | |
| // OUTPUT: | |
| main : Calling test()... | |
| main : Running test()... | |
| test : IN | |
| test : OUT | |
| main : Exiting... | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment