samedi 5 août 2017

Void Promise, Futures vs Condition Variables, Mutex

Please advice on this. I am trying to learn and see if void futures can be instead of condition variables for the below scenario.

From Scott Meyers Effective Modern C++ book, he suggests using void promise and void futures for one shot communication between threads, The waiting thread can have a std::future<void> and call wait() on that and the calling thread can call set_value() on the corresponding std::promise<void>. (For multiple threads, each can have a copy of a void shared future) But he mentions that this can only be used once. What does that mean?

Consider an application where a master thread places a work on each slot of a vector with 3 slots. There are 2 workers per slot and only one can pick the work from a slot. I want the master to notify all the workers every time it fills all 3 slots with new work and sleep for n units of time to allow the workers to finish the work. I need to repeat this, hence can I just pass each workers a copy of a shared future from a promise from the master? As per Scott, promise/ future can be used only once, does that mean once a value for promise has been set, it cant be used again? For this scenario, is condition variable, mutex with an atomic bool a better approach?

Please advice when to use void promise, future vs condition variables, mutex?

Aucun commentaire:

Enregistrer un commentaire