Updated (yet again) Implementation of Futures for C++
Friday, 30 May 2008
I have updated my prototype
futures library implementation yet again. This version adds
wait_for_any() and wait_for_all() functions, which can be used either to wait for up to five futures known
at compile time, or a dynamic collection using an iterator range.
jss::unique_future<int> futures[count];
// populate futures
jss::unique_future<int>* const future=
jss::wait_for_any(futures,futures+count);
std::vector<jss::shared_future<int> > vec;
// populate vec
std::vector<jss::shared_future<int> >::iterator const f=
jss::wait_for_any(vec.begin(),vec.end());
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 and some new features of the Boost.Thread library, which are not available in an official boost release.
Sample usage can be seen in the test harness. The support for alternative allocators is still missing. The documentation for the futures library is available online, but is also included in the zip file.
Please download this prototype, put it through its paces, and let me know what you think.
Posted by Anthony Williams
[/ threading /] permanent link
Tags: futures, promise, threading, concurrency, n2561
Stumble It!
| Submit to Reddit
| Submit to DZone ![]()
If you liked this post, why not subscribe to the RSS feed
or Follow me on Twitter?
2 Comments
Is it possible to put an example in the 'OverView' section? I know what futures are when trading securities, but what futures when taken from a programming perspective?
In this context, a future is a placeholder for a value that will be available at some point in the future. This is one way of communicating values between threads, particularly if you're writing a thread pool. You create a promise or packaged_task, from which you can retrieve a future, and then the work associated with the promise or packaged_task is left to proceed on another thread. When you need the future value you can wait for it, which will block the current thread until the value has been set by the thread doing the work.