线程之间的信号可以通过 std::promise/future 或良好的旧条件变量来实现,有人可以提供示例/用例,其中 1 比其他的更好吗?
我知道 CV 可用于在线程之间多次发出信号,您能否举例说明 std::future/promise 多次发出信号?
std::future::wait_for 的性能也与 std::condition_variable::wait 相同吗?假设我需要作为消费者在队列中等待多个期货,检查每个期货并检查它们是否像下面一样准备好是否有意义?
for(auto it = activeFutures.begin(); it!= activeFutures.end();) {
if(it->valid() && it->wait_for(std::chrono::milliseconds(1)) == std::future_status::ready) {
Printer::print(std::string("+++ Value " + std::to_string(it->get()->getBalance())));
activeFutures.erase(it);
} else {
++it;
}
}
Run Code Online (Sandbox Code Playgroud)