Sunday, 11 May 2008
I have updated my prototype futures library implementation in light of various comments received, and my own thoughts.
The new version is available for download, again under the Boost Software License. It still needs to be compiled against the Boost Subversion Trunk, as it uses the Boost Exception library, which is not available in an official boost release.
Sample usage can be seen in the test harness. The support for alternative allocators is still missing.
- I have removed the
timed_getfunctions, as they can be replaced with a combination of
get(), and they don't work with
- I've also removed the
get()returns an rvalue-reference to allow moving in those types with move support. Yes, if you call
get()twice on a movable type then the second
get()returns an empty shell of an object, but I don't really think that's a problem: if you want to call
get()multiple times, use a
shared_future. I've implemented this with both rvalue-references and the boost.thread move emulation, so you can have a
test_unique_future_for_move_only_udt()in test_futures.cpp shows this in action with a user-defined movable-only type
- Finally, I've added a
set_wait_callback()function to both
packaged_task. This allows for lazy-futures which don't actually run the operation to generate the value until the value is needed: no threading required. It also allows for a thread pool to do task stealing if a pool thread waits for a task that's not started yet. The callbacks must be thread-safe as they are potentially called from many waiting threads simultaneously. At the moment, I've specified the callbacks as taking a non-const reference to the
packaged_taskfor which they are set, but I'm open to just making them be any callable function, and leaving it up to the user to call
bind()to do that.
I've left the wait operations as
timed_wait(), but I've had a suggestion to use
wait_until(), which I'm actively considering.
Please download this prototype, put it through its paces, and let me know what you think.