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.
Semaphore: C++ demo - C++ Tutorial
From the course: Complete Guide to Parallel and Concurrent Programming with C++
Semaphore: C++ demo
- [Instructor] At the time of this recording, in the first half of 2020, the C++ standard library doesn't formally include a semaphore class. That should change in the future when the C++ 20 standard is officially published and fully implemented with compilers, but we're not quite there yet. Until then, the well-known Boost C++ Library does include a semaphore class if you want something ready to use, or you can implement your own semaphore class, which is what we've done for this example, starting on line nine. We built the semaphore class using a mutex, a condition variable and a count variable, which are defined down on lines 31 through 33. The semaphore's constructor function on line 11 accepts the initial value to set the count variable. When you call the semaphore's acquire function on line 15, it initializes a unique lock on the mutex and then uses a while loop to wait if the count has been decreased down to zero. This is similar to the use of a condition variable we saw in…
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.