Semaphores and worker pools 00:00:01 One key on the rack Seven of a workshop's eight machines are running. Two jobs arrive together and both operators see one key hanging on the rack. The observation is true for both operators. What would make it safe for exactly one to start? Taking a key combines the check and the reservation. A semaphore does the same for a limited resource. Job A acquires one permit and the free count becomes zero before another caller can successfully acquire that last permit. Job B now sees a different result. Depending on the contract, it can wait up to a deadline, immediately fail to acquire, or enter a bounded queue. None of those choices adds a ninth machine. 00:00:42 The permit follows the work A completes its thumbnail and releases the permit. Only now is one machine genuinely available for reuse. Releasing as soon as work is dispatched would advertise capacity that is still busy rendering. A waiting B acquires that returned permit and starts. At every transition, the number of permit holders stays at or below eight. A semaphore does not automatically guarantee which waiting caller will be chosen first. What if B's renderer throws an exception? Release the permit on every exit after acquisition, including cancellation once the underlying work has truly stopped. Otherwise a failed thumbnail can quietly remove a machine from useful capacity. 00:01:25 One lock does a different job Two workers may update the same waiting list at once. A mutex can protect that short modification by allowing one editor at a time. It answers a different question from the eight permits protecting the expensive machines. Holding that one mutex for the whole rendering operation would serialize work that the eight machines could perform independently. Protect the list transition, then let admitted jobs run outside that short critical section. Reading the available-count metric is useful for a dashboard. It is unsafe as a separate permission to start. Two callers can both see one, taking us straight back to the two operators by the rack. 00:02:04 Limit the waiting room too Eight workers protect the machines, but thousands more callers might wait while holding memory and connections. An unlimited waiting room merely shifts overload from the expensive GPU to the rest of the service. Set a queue limit with a deadline. When it fills, reject or defer new work in a way the caller understands. Record waiting time as well as queue length, because an old job can be stuck in a short queue. More waiters cannot improve throughput once all machines are busy. Compare arrivals with successful completions and watch how long jobs occupy a permit. That tells the operator whether to add machines or fix unusually slow work. 00:02:45 Five racks in front of eight machines Now start five dispatcher processes, each with its own eight-permit semaphore. How many jobs can they admit together? Forty. The hardware pool still contains only eight machines. Make the admission decision where it owns the actual pool, or assign processes disjoint shares whose total stays within eight. The right coordination boundary follows the resource being protected. The two new operators can both read the same dashboard. Only one holds the key. That distinction between seeing capacity and reserving capacity is the reason the ninth job never starts.