A helper class to facilitate waiting for and/or getting the results of multiple futures at once.
More...
#include <BS_thread_pool.hpp>
|
| std::conditional_t< std::is_void_v< T >, void, std::vector< T > > | get () |
| | Get the results from all the futures stored in this BS::multi_future, rethrowing any stored exceptions.
|
| |
| std::size_t | ready_count () const |
| | Check how many of the futures stored in this BS::multi_future are ready.
|
| |
| bool | valid () const noexcept |
| | Check if all the futures stored in this BS::multi_future are valid.
|
| |
| void | wait () const |
| | Wait for all the futures stored in this BS::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 BS::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 BS::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 BS::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.
471 {
472 if constexpr (std::is_void_v<T>)
473 {
474 for (std::future<T>& future : *this)
476 return;
477 }
478 else
479 {
480 std::vector<T> results;
481 results.reserve(this->
size());
482 for (std::future<T>& future : *this)
483 results.push_back(future.
get());
484 return results;
485 }
486 }
std::conditional_t< std::is_void_v< T >, void, std::vector< T > > get()
Get the results from all the futures stored in this BS::multi_future, rethrowing any stored exception...
Definition BS_thread_pool.hpp:470
GOpaque< Size > size(const GMat &src)
◆ ready_count()
Check how many of the futures stored in this BS::multi_future are ready.
- Returns
- The number of ready futures.
494 {
495 std::size_t count = 0;
496 for (const std::future<T>& future : *this)
497 {
498 if (future.wait_for(std::chrono::duration<double>::zero()) == std::future_status::ready)
499 ++count;
500 }
501 return count;
502 }
◆ valid()
Check if all the futures stored in this BS::multi_future are valid.
- Returns
true if all futures are valid, false if at least one of the futures is not valid.
510 {
511 bool is_valid = true;
512 for (const std::future<T>& future : *this)
513 is_valid = is_valid && future.
valid();
514 return is_valid;
515 }
bool valid() const noexcept
Check if all the futures stored in this BS::multi_future are valid.
Definition BS_thread_pool.hpp:509
◆ wait()
Wait for all the futures stored in this BS::multi_future.
521 {
522 for (const std::future<T>& future : *this)
524 }
void wait() const
Wait for all the futures stored in this BS::multi_future.
Definition BS_thread_pool.hpp:520
◆ 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 BS::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.
536 {
537 const std::chrono::time_point<std::chrono::steady_clock> start_time = std::chrono::steady_clock::now();
538 for (const std::future<T>& future : *this)
539 {
540 future.wait_for(duration - (std::chrono::steady_clock::now() - start_time));
541 if (duration < std::chrono::steady_clock::now() - start_time)
542 return false;
543 }
544 return true;
545 }
◆ 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 BS::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.
557 {
558 for (const std::future<T>& future : *this)
559 {
560 future.wait_until(timeout_time);
561 if (timeout_time < C::now())
562 return false;
563 }
564 return true;
565 }
The documentation for this class was generated from the following file: