So far, we know how to use both POSIX and C++ standard library mechanisms to synchronize a critical section. There are use cases where we don't need to explicitly use locks; instead, we can use more simple communication mechanisms. std::promise and std::future can be used to allow two threads to communicate without the hassle of the synchronization.
Learning inter-thread communication with simple events
How to do it...
In this recipe, we will write a program that splits a problem into two parts: thread 1 will run a highly intensive computation and will send the result to thread 2, which is the consumer of the results. We'll do this by using std::promise and std::future. Let's get started:
- Open...