From the course: Complete Guide to Parallel and Concurrent Programming with C++

Unlock this course with a free trial

Join today to access over 24,800 courses taught by industry experts.

Barrier: C++ demo

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're 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 Baron 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, we'll need to include the boost thread barrier header file at the…

Contents