From the course: Parallel and Concurrent Programming with C++ Part 2

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Barrier: C++ demo

Barrier: C++ demo - C++ Tutorial

From the course: Parallel and Concurrent Programming with C++ Part 2

Start my 1-month free trial

Barrier: C++ demo

- [Instructor] At the time of this recording, in early 2020, barriers were an experimental part of the C++ standard. Though it looks like they expected to be included as part of the official C++ 20 standard. But, until that happens, and it gets implemented in common compiler libraries, we can turn to the well-known Boost collection of open source C++ libraries, which conveniently does have a barrier we can use. To demonstrate using a barrier in C++, we'll build on the previous example that demonstrated a race condition by creating 10 shopper threads named Olivia and Barron that either added or multiplied the number of chips to buy. Without a barrier in place, this program has a race condition that produces a different final result each time we run it. So, let's use a barrier to make sure all five of the Olivia shopper threads execute their add operation before the five Barron shoppers multiply the bags of chips. First…

Contents