Skip to content

Instantly share code, notes, and snippets.

@chandraprakash
Created April 2, 2014 13:06
Show Gist options
  • Save chandraprakash/9933694 to your computer and use it in GitHub Desktop.
Save chandraprakash/9933694 to your computer and use it in GitHub Desktop.
Test wait_for() for boost and std future (promise version)
#include <chrono>
#include <future>
#include <iostream>
#include <string>
#include <thread>
#include "boost/thread/future.hpp"
int main() {
{
std::cout << " -- Boost Version -- " << std::endl;
boost::promise<void> boost_promise;
auto boost_future = boost_promise.get_future();
auto a1 = boost::async(boost::launch::async, [&boost_promise] {
std::cout << " -- Sleep Now -- " << std::endl;
boost::this_thread::sleep_for(boost::chrono::seconds(5));
std::cout << " -- Awake Now -- " << std::endl;
boost_promise.set_value();
});
if (boost_future.wait_for(boost::chrono::seconds(10)) == boost::future_status::ready)
std::cout << "future_status::ready for boost future" << std::endl;
else
std::cout << "future_status:: not ready for boost future" << std::endl;
}
{
std::cout << " -- Std Version -- " << std::endl;
std::promise<void> std_promise;
auto std_future = std_promise.get_future();
auto a1 = std::async(std::launch::async, [&std_promise] {
std::cout << " -- Sleep Now -- " << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(5));
std::cout << " -- Awake Now -- " << std::endl;
std_promise.set_value();
});
if (std_future.wait_for(std::chrono::seconds(10)) == std::future_status::ready)
std::cout << "future_status::ready for std future" << std::endl;
else
std::cout << "future_status:: not ready for std future" << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment