Can I use the same std::promise
and std::future
objects multiple times?
For example, I want to send several values many times from Thread-1 to Thread-2. Can I use promise/future multiple times, and how to do it thread-safety?
std::promise<int> send_value;
std::future<int> receive_value = send_value.get_future();
std::thread t1 = std::thread([&]()
{
while (!exit_flag) {
int value = my_custom_function_1();
send_value.set_value(value);
}
});
std::thread t2 = std::thread([&]()
{
while (!exit_flag) {
int value = receive_value.get();
my_custom_function_2(value);
}
});