Easy
std::counting_semaphore limiting concurrent connections
openConnection() is called by 10 threads concurrently, but the service must never have more than 4 connections open at once. Using std::counting_semaphore<8> initialized to 4 as shown, what behavior does this actually produce, and how does it differ fundamentally from guarding the same code with a plain std::mutex?
main.cpp
std::counting_semaphore<8> connectionSlots(4); // supports >= 8 permits, starts with 4
void openConnection(int id) {
connectionSlots.acquire(); // blocks if 4 permits are already taken
doNetworkWork(id); // at most 4 of these run concurrently
connectionSlots.release(); // frees one permit back up
}
// 10 threads each call openConnection(id) once41 of 51 in Concurrency