-
-
Notifications
You must be signed in to change notification settings - Fork 420
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed #795: Create timed make_ready_future, adding example demonstrat…
…ing new facilities
- Loading branch information
Showing
5 changed files
with
268 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright (c) 2013 Hartmut Kaiser | ||
// | ||
// Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
|
||
// This example demonstrates the use of the utility function | ||
// make_ready_future_after to orchestrate timed operations with 'normal' | ||
// asynchronous work. | ||
|
||
#include <hpx/hpx_init.hpp> | ||
#include <hpx/include/iostreams.hpp> | ||
#include <hpx/util/high_resolution_timer.hpp> | ||
|
||
#include <boost/date_time/posix_time/posix_time.hpp> | ||
|
||
/////////////////////////////////////////////////////////////////////////////// | ||
void wake_up_after_2_seconds() | ||
{ | ||
hpx::cout << "waiting for 2 seconds\n"; | ||
|
||
hpx::util::high_resolution_timer t; | ||
|
||
// Schedule a wakeup after 2 seconds. | ||
using boost::posix_time::seconds; | ||
hpx::future<void> f = hpx::make_ready_future_after(seconds(2)); | ||
|
||
// ... do other things while waiting for the future to get ready | ||
|
||
// wait until the new future gets ready | ||
f.wait(); | ||
|
||
hpx::cout << "woke up after " << t.elapsed() | ||
<< " seconds\n" << hpx::flush; | ||
} | ||
|
||
int return_int_at_time() | ||
{ | ||
hpx::cout << "generating an 'int' value 2 seconds from now\n"; | ||
|
||
hpx::util::high_resolution_timer t; | ||
|
||
// Schedule a wakeup 2 seconds from now. | ||
using namespace boost::posix_time; | ||
int arg = 42; | ||
hpx::future<int> f = hpx::make_ready_future_at( | ||
microsec_clock::universal_time() + seconds(2), 42); | ||
|
||
// ... do other things while waiting for the future to get ready | ||
|
||
// wait until the new future gets ready (should return 42) | ||
int retval = f.get(); | ||
|
||
hpx::cout << "woke up after " << t.elapsed() | ||
<< " seconds, returned: " << retval << "\n" | ||
<< hpx::flush; | ||
|
||
return retval; | ||
} | ||
|
||
/////////////////////////////////////////////////////////////////////////////// | ||
int hpx_main() | ||
{ | ||
wake_up_after_2_seconds(); | ||
return_int_at_time(); | ||
return hpx::finalize(); | ||
} | ||
|
||
/////////////////////////////////////////////////////////////////////////////// | ||
int main(int argc, char* argv[]) | ||
{ | ||
// Initialize and run HPX. | ||
return hpx::init(argc, argv); | ||
} | ||
|
This file contains 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
This file contains 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
This file contains 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