Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I "clear" std::promise?

I have a program, which should in cycle launch 8 threads, which will return a value using std::promise. So I think I need to create a vector of 8 promise objects, get their futures, and use these promises to return the values and then join the threads with main. The problem is this: on the next iteration I will create 8 more threads -- can i reuse the same promise objects, or do I need to create 8 more? I haven't found any way to reuse them on the internet, but maybe I'm missing something obvious?

like image 883
Akiiino Avatar asked Feb 03 '16 10:02

Akiiino


People also ask

Can STD promise be reused?

Note that the std::promise object is meant to be used only once.

What is a promise in C++?

A promise is an object that can store a value of type T to be retrieved by a future object (possibly in another thread), offering a synchronization point. On construction, promise objects are associated to a new shared state on which they can store either a value of type T or an exception derived from std::exception .


1 Answers

To reuse promises, simply reassign them.

std::promise<int> my_promise;
//use the promise
my_promise = std::promise<int>(); //now you have a new promise
like image 178
The Quantum Physicist Avatar answered Oct 11 '22 07:10

The Quantum Physicist