Skip to content

Instantly share code, notes, and snippets.

@juehan
Created January 25, 2013 02:50
Show Gist options
  • Save juehan/4631329 to your computer and use it in GitHub Desktop.
Save juehan/4631329 to your computer and use it in GitHub Desktop.
#include <future>
#include <iostream>
#include <chrono>
//Speculative execution using std::future<> wait_for()
int quick_but_gestimation()
{
for (int i=0; i < 2; i++)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << "[quick func]: time elapsed : " << i << " second" << std::endl;
}
return 10;
}
int slow_but_accurate()
{
for (int i=0; i < 6; i++)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << "[slow func]: time elapsed : " << i << " second" << std::endl;
}
return 11;
}
std::future<int> f;
int bestResult()
{
f = std::async(std::launch::async, slow_but_accurate);
int guestimation = quick_but_gestimation();
std::future_status s = f.wait_for(std::chrono::seconds(3));
if(s == std::future_status::ready)
return f.get();
else
return guestimation;
}
int main()
{
std::cout << "Best result: " << std::endl;
std::cout << bestResult() << std::endl;
}
/*
//output from g++ 4.7.2
// When future object is declared and defined INSIDE bestResult() function
Best result:
[slow func]: time elapsed : 0 second[quick func]: time elapsed :
0 second
[quick func]: time elapsed : 1 second
[slow func]: time elapsed : 1 second
[slow func]: time elapsed : 2 second
[slow func]: time elapsed : 3 second
[slow func]: time elapsed : 4 second
[slow func]: time elapsed : 5 second
10
Press ENTER to continue...
// When future object is declared and defined OUTSIDE bestResult() function
Best result:
[slow func]: time elapsed : [quick func]: time elapsed : 0 second
0 second
[slow func]: time elapsed : 1 second
[quick func]: time elapsed : 1 second
[slow func]: time elapsed : 2 second
[slow func]: time elapsed : 3 second
10
[slow func]: time elapsed : 4 second
[slow func]: time elapsed : 5 second
Press ENTER to continue...
*/
@juehan
Copy link
Author

juehan commented Jan 25, 2013

Be cautious VS2012 has notable behavioral difference with gcc 4.7, which is that both cases works exactly same regardless of location of future object. Another difference is enum class std::future_status should be written as std::future_status::future_status or it's typedef std::_Future_status in VC++11.

@juehan
Copy link
Author

juehan commented Jan 25, 2013

GCC4.7

  /// Launch code for futures
  enum class launch 
  { 
    any, 
    async, 
    sync 
  };

  /// Status code for futures
  enum class future_status 
  {
    ready,
    timeout,
    deferred
  };

VS2012

namespace launch {
enum launch {   // names for launch options passed to async
    async = 0x1,
    deferred = 0x2,
    any = async | deferred, // retained
    sync = deferred
    };


...

}

typedef launch::launch _Launch_type;



    namespace future_status {
enum future_status {    // names for timed wait function returns
    ready,
    timeout,
    deferred
    };
    }   // namespace future_status

typedef future_status::future_status _Future_status;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment