Thread Synchronization with Condition Variables or Tasks

Contents[Show]

In case you use promise and future to synchronize threads, they have much in common with condition variables. But most of the time, tasks 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).

ConditionVariableVersusTask

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 if you use the condition variable only for one synchronization, the condition variable is a lot more challenging to use correctly. So a promise and future pair need no shared variable and, therefore, no lock; they are not prone to spurious or lost wakeups. In addition to that, they can handle exceptions. So there are a lot of reasons to prefer tasks to condition variables.

 

Rainer D 6 P2 540x540Modernes C++ Mentoring

Be part of my mentoring programs:

 

 

 

 

Do you want to stay informed about my mentoring programs: Subscribe via E-Mail.

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 (line 32), I get the future fut (line 34). The promise signalizes, in this case by his return value void (std::promise<void> sendReady), that it can only send notifications. Both communications endpoints are moved into the threads t1 respectively t2 (lines 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).

Also, the structure as the program's output matches the program in den post about the condition variable.

promiseFutureSynchronize

What's next?

So that was the overview of the multithreading interface. Now it's time to have a deeper look into multithreading in C++. Although most programmers will and should never use the features of the C++ memory model, it gives you a deeper insight into the challenges of multithreading 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, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, and Rob North.

 

Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, and Slavko Radman.

 

 

My special thanks to Embarcadero CBUIDER STUDIO FINAL ICONS 1024 Small

 

My special thanks to PVS-Studio PVC Logo

 

My special thanks to Tipi.build tipi.build logo

 

My special thanks to Take Up code TakeUpCode 450 60

 

Seminars

I'm happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.

Bookable (Online)

German

Standard Seminars (English/German)

Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.

  • C++ - The Core Language
  • C++ - The Standard Library
  • C++ - Compact
  • C++11 and C++14
  • Concurrency with Modern C++
  • Design Pattern and Architectural Pattern with C++
  • Embedded Programming with Modern C++
  • Generic Programming (Templates) with C++

New

  • Clean Code with Modern C++
  • C++20

Contact Me

Modernes C++,

RainerGrimmDunkelBlauSmall

 

Comments   

0 #21 Hanna 2017-03-05 19:23
Everything is very open with a very clear description of
the challenges. It was definitely informative. Your website is useful.
Many thanks for sharing!
Quote
0 #22 Katia 2017-09-12 01:25
This iis really interesting, You're a very skilled
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!
Quote

Stay Informed about my Mentoring

 

Mentoring

English Books

Course: Modern C++ Concurrency in Practice

Course: C++ Standard Library including C++14 & C++17

Course: Embedded Programming with Modern C++

Course: Generic Programming (Templates)

Course: C++ Fundamentals for Professionals

Course: The All-in-One Guide to C++20

Course: Master Software Design Patterns and Architecture in C++

Subscribe to the newsletter (+ pdf bundle)

All tags

Blog archive

Source Code

Visitors

Today 4061

Yesterday 4371

Week 39868

Month 169993

All 12057759

Currently are 226 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments