1 year ago
#340899
Yasiru Lelwala
Parallel construction/instantiation in c++
I have a class that has many setter methods.
Each subsequent setter method requires the data that has been set by the preceding setter method. i.e. if setA()
has set the variable _a
in the class, then setB()
will require the variable _a
If not initialised in the constructor's initialisation list, I would have to call the methods after the object has been instantiated.
I have a significant number of objects on which to call these methods. Thus, I would call the methods on those objects in parallel using std::async()
.
The problem with calling std::async(std::launch::async, &SomeClass::setB, std::ref(objectOfSomeClass))
is that it requires _a
to have been already set by setA()
. This may not be the case given the random order in which tasks are executed.
But, i figure it would be easier to invoke the setters in the initialisation list. i.e. SomeClass() : _a(setA()), _b(setB()) {}
and parallelise the construction instead.
Is this thinking correct? Is there a better way to do it?
I think one alternative is to create a single method that invokes the setters(void run() {this->_a = setA(); this->_b = setB();}
), and call that using std::async()
in main.
How can I construct objects using std::async()
and then store them in a vector so that I can access data from the objects that i have created using tasks?
c++
c++17
stdthread
stdasync
0 Answers
Your Answer