In case you are using promise and future to synchronize threads, they have a lot in common with condition variables. But most of the time, task are the better choice.
Synchronization of threads
To get the big picture, get the facts. The table compares condition variables with tasks (promise and future).

The benefit of a condition variable to a promise and future is, that you can use condition variables to synchronize threads multiple times. In opposite to that, a promise can send its notification only once. So you have to use more promise and future pairs to get the functionality of a condition variable. But in case you use the condition variable only for one synchronization, the condition variable is a lot more difficult to use in right way. So a promise and future pair needs no shared variable and therefore no lock, they are not prone to spurious wakeups or lost wakeups. In addition to that, they can handle exceptions. So there are lot of reasons to prefer tasks to condition variables.
How can I use tasks to synchronize threads?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
// promiseFutureSynchronize.cpp
#include <future>
#include <iostream>
#include <utility>
void doTheWork(){
std::cout << "Processing shared data." << std::endl;
}
void waitingForWork(std::future<void>&& fut){
std::cout << "Worker: Waiting for work." << std::endl;
fut.wait();
doTheWork();
std::cout << "Work done." << std::endl;
}
void setDataReady(std::promise<void>&& prom){
std::cout << "Sender: Data is ready." << std::endl;
prom.set_value();
}
int main(){
std::cout << std::endl;
std::promise<void> sendReady;
auto fut= sendReady.get_future();
std::thread t1(waitingForWork,std::move(fut));
std::thread t2(setDataReady,std::move(sendReady));
t1.join();
t2.join();
std::cout << std::endl;
}
|
Quite easy.
With the help of the promise sendReady (line32) I get the future fut (line34). The promise signalises in this case by his return value void (std::promise<void> sendReady), that it is only capable of sending notifications. Both communications endpoints are moved into the thread t1 respectively t2 (line 35 and 36). The future is waiting with its call fut.wait() (line 15) for the notification of the promise: prom.set_value() (line 24).
As well the structure as the output of the program matches the program in den post about the condition variable.

What's next?
So that was the overview of the mulitithreading interface. Now it's time to have a deeper look into mulithreading in C++. Although the most programmers will and should never use the features of C++ memory model, it gives you a deeper insight into the challenges of mulitthrading programming. The next post starts with an overview of the C++ memory model. (Proofreader Alexey Elymanov)
-
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, Marko, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Darshan Mody, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, espkk, Wolfgang Gärtner, Louis St-Amour, Stephan Roslen, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Avi Kohn, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Neil Wang, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, and Tobi Heideman.
Thanks in particular to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, and Richard Sargeant.
My special thanks to Embarcadero 
Seminars
I'm happy to give online-seminars or face-to-face seminars world-wide. Please call me if you have any questions.
Bookable (Online)
Deutsch
English
Standard Seminars
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
New
Contact Me
Modernes C++,

Comments
the challenges. It was definitely informative. Your website is useful.
Many thanks for sharing!
blogger. I have joined your rss feed aand look forward to
seeking more of your great post. Also, I have shared your
website in my social networks!
RSS feed for comments to this post