Is it possible to block on a group of locks/futures/any blockable entities, until any one of them is ready? The idea is that we can do:
std::vector<std::future<T>> futures = ...;
auto ready_future = wait_until_an_element_is_ready(futures);
process(ready_future.get());
I remember libraries like libevent, libev, and libuv have this kind of ability for IO tasks. But I don't know if these can be done for locks/futures.
A way I thought of to sort of achieve this is to have the futures call a handler upon completion, but at the same time compare-and-exchange the handler to null so that no other futures can call it. This requires the coordination of the futures, however, so can't be done for locks.
UPDATE: There seems to be a proposal for it for C++2x.
If you have boost available, its threading capabilities far exceed the standard library.
This will be the case after c++17, and by the looks of things, even after c++20.
#include <future>
#include <vector>
#define BOOST_THREAD_VERSION 4
#include <boost/thread.hpp>
void wait_for_any(std::vector<boost::future<void>> & v)
{
boost::wait_for_any(std::begin(v), std::end(v));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With