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,900 courses taught by industry experts.
Latch: C++ demo - C++ Tutorial
From the course: Complete Guide to Parallel and Concurrent Programming with C++
Latch: C++ demo
- [Instructor] In addition to barriers to control the order of execution, there's another related synchronization mechanism called a latch. This allows one or more threads to wait until a set of operations being performed in other threads to complete. The latch is initialized with a given count value, which threads can use in two ways. Threads can either wait at a latch until the count value reaches zero. Much like how threads can wait at a barrier. Or they can decrement the count value by calling the latch's countdown function. So, the key difference between a latch and a barrier is that the barrier releases when a certain number of threads waiting on it has been reached. Whereas the latch releases after the countdown function gets called enough times to reduce the count value to zero. For this demonstration, we'll modify the previous example code that used a barrier to now use a latch instead. First, we'll change the include statement on line 6 to include the latch header file from…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.