One Time Synchronization of Threads

Explore one-time synchronization of threads with examples.

Sender-receiver workflows are quite common for threads. In such a workflow, the receiver is waiting for the sender’s notification before Future continues to work. There are various ways to implement these workflows. With C++11, you can use condition variables or promise/future pairs; with C++20, you can use std::atomic_flag. Each way has its pros and cons. Consequently, I want to compare them. I assume you don’t know the details of condition variables or promises and futures. Therefore, I provide a short refresher.

Condition variables

A condition variable can fulfill the role of a sender or a receiver. As a sender, it can notify one or more receivers.

Run the following program 4 times and notice the output.

Get hands-on with 1200+ tech skills courses.