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? You can also subscribe to this blog by email using the form on the left.
Design and Content Copyright © 2005-2024 Just Software Solutions Ltd. All rights reserved. | Privacy Policy
3 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.
Hi,
I am reading the source code of boost1.4.1/thread/future, it's quite enlightening.
But I have question when trying to understand the implementation of wait_for_any; w hen construct the all_futures_lock, the constructor will apply following operation:
for(unsigned i=0;i<count;++i) { locks[i]=boost::unique_lock<boost::mutex>(futures[i].future->mutex); }
it seems just lock future's mutex one by one, without defer_lock or using the boost::lock algorithm, would it cause deadlock when I call wait_for_any(f1, f2) in one thread and wait_for_any(f2, f1) in other thread? If won't, why?
Thank you very much! Zudeng