Class template future_selector
poet::future_selector — efficient repeated waits on a group of futures
Synopsis
template<typename T> class future_selector { public: // types typedef future<T> value_type; typedef std::size_t size_type; typedef std::ptrdiff_t difference_type; // construct/copy/destruct future_selector(); future_selector(const future_selector &); future_selector& operator=(const future_selector &); ~future_selector(); // public member functions future<T> selected() const; void pop_selected(); void push(const future<T> &); template<typename Converter, typename U> void push(const Converter &, const ExceptionHandler &, const future<U> &); difference_type size() const; void reset(); void swap(future_selector &); }; // free functions template<typename T> void swap(future_selector<T> &, future_selector<T> &);
Description
future_selector
allows you to obtain futures which will become ready when
any future in a group of futures becomes ready. It is more efficient than maintaining
a separate container of futures and repeatedly passing them to
future_select_range.
Most of the methods of future_selector
are thread-safe. The exceptions
to this rule are:
reset, swap, and the assignment operator.
See also
-
future_select: construct a future which becomes ready when any of a group of futures is ready or has an exception.
-
future_barrier: construct a future which becomes ready when all of a group of futures are ready or one has an exception.
future_selector
public construct/copy/destruct
-
future_selector();
Constructs a future_selector containing no futures. Futures may be added to the
future_selector
with thepush
method.Postconditions:
size() == 0
-
future_selector(const future_selector & other);
*this
will receive a copy of all the futures inother
. Copy construction has deep copy semantics, so calling methods such aspush
orpop_selected
on a future_selector copy will not effect the original, and vice-versa.Postconditions:
this->size() == other.size()
-
future_selector& operator=(const future_selector & rhs);
Copy-constructs a temporary
future_selector
fromrhs
, then swaps it with*this
.Postconditions:
this->size() == rhs.size()
-
~future_selector();
Any futures contained in the
future_selector
at its time of destruction will be kept alive as long as needed and to fulfill any remaining "selected futures" previously obtained from calls to theselected
method, even after thefuture_selector
itself has been destroyed. However, any "selected futures" which no longer have any possibility of being fulfilled, due to an insufficient number of futures contained in thefuture_selector
at the time of its destruction (that is,size
returns a negative value), will be automatically reneged with anuncertain_future
exception.
future_selector
public member functions
-
future<T> selected() const;
Returns a future which will receive the value or exception of the next future in the
future_selector
to complete. The "selected future" returned will be fulfilled only after any selected futures obtained prior to the lastpop_selected
call are fulfilled. -
void pop_selected();
Removes a future from the
future_selector
, so subsequent calls toselected
will return a different "selected future". The selected futures will be fulfilled in the order they were popped out of thefuture_selector
.This method may be called before the future returned by
selected
is actually complete. It may even be called more times than the number of futures which have been pushed into thefuture_selector
with thepush
method. This results insize
returning a negative value, until more futures are pushed into thefuture_selector
. -
void push(const future<T> & f); template<typename Converter, typename U> void push(const Converter & converter, const ExceptionHandler & exception_handler, const future<U> & f);
Adds a future to the
future_selector
. When the added future becomes ready or gets an exception, it will be used to fulfill or renege the oldest "selected future" which has not yet completed.The 3 argument overload of this function is provided for convenience, and is equivalent to
push(poet::future_combining_barrier<T>(converter, exception_handler, f))
. -
difference_type size() const;
Returns:
The number of times push has been called, minus the number of times pop_selected has been called, since the
future_selector
was default-constructed or reset. This value may be negative. -
void reset();
Default-constructs a temporary
future_selector
and swaps it with*this
.Postconditions:
size() == 0
-
void swap(future_selector & other);
Swaps
*this
withother
.
future_selector
free functions
-
template<typename T> void swap(future_selector<T> & a, future_selector<T> & b);
Swaps
future_selector
s a and b.