A helper class to facilitate waiting for and/or getting the results of multiple futures at once.
More...
#include <BS_thread_pool.hpp>
|
| multi_future (const multi_future &)=delete |
|
multi_future & | operator= (const multi_future &)=delete |
|
| multi_future (multi_future &&)=default |
|
multi_future & | operator= (multi_future &&)=default |
|
std::conditional_t< std::is_void_v< T >, void, std::vector< T > > | get () |
| Get the results from all the futures stored in this multi_future , rethrowing any stored exceptions.
|
|
size_t | ready_count () const |
| Check how many of the futures stored in this multi_future are ready.
|
|
bool | valid () const |
| Check if all the futures stored in this multi_future are valid.
|
|
void | wait () const |
| Wait for all the futures stored in this multi_future .
|
|
template<typename R , typename P > |
bool | wait_for (const std::chrono::duration< R, P > &duration) const |
| Wait for all the futures stored in this multi_future , but stop waiting after the specified duration has passed. This function first waits for the first future for the desired duration. If that future is ready before the duration expires, this function waits for the second future for whatever remains of the duration. It continues similarly until the duration expires.
|
|
template<typename C , typename D > |
bool | wait_until (const std::chrono::time_point< C, D > &timeout_time) const |
| Wait for all the futures stored in this multi_future , but stop waiting after the specified time point has been reached. This function first waits for the first future until the desired time point. If that future is ready before the time point is reached, this function waits for the second future until the desired time point. It continues similarly until the time point is reached.
|
|
template<typename T>
class BS::multi_future< T >
A helper class to facilitate waiting for and/or getting the results of multiple futures at once.
- Template Parameters
-
T | The return type of the futures. |
◆ get()
template<typename T >
std::conditional_t< std::is_void_v< T >, void, std::vector< T > > BS::multi_future< T >::get |
( |
| ) |
|
|
inline |
Get the results from all the futures stored in this multi_future
, rethrowing any stored exceptions.
- Returns
- If the futures return
void
, this function returns void
as well. Otherwise, it returns a vector containing the results.
188 {
189 if constexpr (std::is_void_v<T>)
190 {
191 for (std::future<T>& future : *this)
193 return;
194 }
195 else
196 {
197 std::vector<T> results;
198 results.reserve(this->
size());
199 for (std::future<T>& future : *this)
200 results.push_back(future.
get());
201 return results;
202 }
203 }
std::conditional_t< std::is_void_v< T >, void, std::vector< T > > get()
Get the results from all the futures stored in this multi_future, rethrowing any stored exceptions.
Definition BS_thread_pool.hpp:187
GOpaque< Size > size(const GMat &src)
◆ ready_count()
Check how many of the futures stored in this multi_future
are ready.
- Returns
- The number of ready futures.
211 {
212 size_t count = 0;
213 for (const std::future<T>& future : *this)
214 {
215 if (future.wait_for(std::chrono::duration<double>::zero()) == std::future_status::ready)
216 ++count;
217 }
218 return count;
219 }
◆ valid()
Check if all the futures stored in this multi_future
are valid.
- Returns
true
if all futures are valid, false
if at least one of the futures is not valid.
227 {
228 bool is_valid = true;
229 for (const std::future<T>& future : *this)
230 is_valid = is_valid && future.
valid();
231 return is_valid;
232 }
bool valid() const
Check if all the futures stored in this multi_future are valid.
Definition BS_thread_pool.hpp:226
◆ wait()
Wait for all the futures stored in this multi_future
.
238 {
239 for (const std::future<T>& future : *this)
241 }
void wait() const
Wait for all the futures stored in this multi_future.
Definition BS_thread_pool.hpp:237
◆ wait_for()
template<typename T >
template<typename R , typename P >
bool BS::multi_future< T >::wait_for |
( |
const std::chrono::duration< R, P > & |
duration | ) |
const |
|
inline |
Wait for all the futures stored in this multi_future
, but stop waiting after the specified duration has passed. This function first waits for the first future for the desired duration. If that future is ready before the duration expires, this function waits for the second future for whatever remains of the duration. It continues similarly until the duration expires.
- Template Parameters
-
R | An arithmetic type representing the number of ticks to wait. |
P | An std::ratio representing the length of each tick in seconds. |
- Parameters
-
duration | The amount of time to wait. |
- Returns
true
if all futures have been waited for before the duration expired, false
otherwise.
253 {
254 const std::chrono::time_point<std::chrono::steady_clock> start_time = std::chrono::steady_clock::now();
255 for (const std::future<T>& future : *this)
256 {
257 future.wait_for(duration - (std::chrono::steady_clock::now() - start_time));
258 if (duration < std::chrono::steady_clock::now() - start_time)
259 return false;
260 }
261 return true;
262 }
◆ wait_until()
template<typename T >
template<typename C , typename D >
bool BS::multi_future< T >::wait_until |
( |
const std::chrono::time_point< C, D > & |
timeout_time | ) |
const |
|
inline |
Wait for all the futures stored in this multi_future
, but stop waiting after the specified time point has been reached. This function first waits for the first future until the desired time point. If that future is ready before the time point is reached, this function waits for the second future until the desired time point. It continues similarly until the time point is reached.
- Template Parameters
-
C | The type of the clock used to measure time. |
D | An std::chrono::duration type used to indicate the time point. |
- Parameters
-
timeout_time | The time point at which to stop waiting. |
- Returns
true
if all futures have been waited for before the time point was reached, false
otherwise.
274 {
275 for (const std::future<T>& future : *this)
276 {
277 future.wait_until(timeout_time);
278 if (timeout_time < std::chrono::steady_clock::now())
279 return false;
280 }
281 return true;
282 }
The documentation for this class was generated from the following file: