Ежедневный бит(е) C++ #246, Общая версия будущего, std::shared_future.
std::shared_future — это инструмент синхронизации C++11, подходящий для однократных ситуаций с одним производителем и многими потребителями.
В отличие от std::future, std::shared_future можно копировать, что позволяет нескольким экземплярам std::shared_future ссылаться на одно и то же общее состояние.
Подобно std::future, std::shared_future‹void>< можно использовать для сигнализации.
#include <future> #include <thread> #include <vector> std::promise<int> provider; // Transfer the state from the promise generated future // to a shared future. std::shared_future<int> future(provider.get_future()); std::vector<std::jthread> runners; // Start a new thread, taking a copy of the future. runners.push_back(std::jthread([future](){ // Block until the promise is fulfilled. int value = future.get(); // Process the result. })); // Start a new thread, taking a copy of the future. runners.push_back(std::jthread([future](){ // Block until the promise is fulfilled. int value = future.get(); // Process the result. })); // Fulfill the promise provider.set_value(42);