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.

Semaphore: C++ demo

Semaphore: C++ demo - C++ Tutorial

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

Start my 1-month free trial

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 line 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…

Contents