Skip to content

Instantly share code, notes, and snippets.

@iamazeem
Created April 19, 2019 06:31
Show Gist options
  • Select an option

  • Save iamazeem/eb089682c0f92203d28d315304b523fe to your computer and use it in GitHub Desktop.

Select an option

Save iamazeem/eb089682c0f92203d28d315304b523fe to your computer and use it in GitHub Desktop.
#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