AlgoViz

Sharing a worker pool safely

medium 6 min

A thumbnail service has eight GPU slots.

Video lesson · Chapter 1 of 5

One key on the rack

0:00 / 3:21

Semaphore

1:04Reel

Thread pool

1:03Reel

Theory

Read or listen first. About 1 min.

What it is

A slot is a promise that one job can occupy a physical resource until it finishes. Imagine eight numbered hooks beside a workshop door. A worker takes a key from a hook before using a machine and returns it when the machine is idle. Counting the hooks from across the room cannot reserve a machine. Another worker can take the last key while you walk to the door.

Eight permits protect eight workers

Active jobs on the GPU pool12345678Job 9 waitsNo permit freeCapacity = 8 · Running = 8 · Spare = 0
Eight jobs hold the eight permits. Job 9 waits at admission while every worker is busy.

Why it exists

A limit and a queue solve different problems. Eight slots bound active work. A queue of 100 bounds how many jobs the service agrees to keep waiting. Without the second bound, a burst can exhaust memory while the GPU remains perfectly within its limit.

View diagram · GPU admission
Request → Bounded queue (enqueue); Bounded queue → Permit pool (wait); Permit pool → GPU job (admit); GPU job → Completion (finish); Completion → Permit pool (return permit)
Release capacity when the physical work actually ends.
Full size

Words to know

Permit
An admission right to occupy one unit of a limited resource.
Backpressure
A signal that limits incoming work when a downstream stage is saturated.

How it works

Step through the idea, or press Play.

Step 1 of 3

Protect the transition. A mutex around the entire check and claim gives concurrent handlers a single order. A read lock can serve a status snapshot, but a later claim must check capacity again under exclusive access.

Understand it

Start here

The workshop key rack

Picture eight numbered keys on hooks beside a workshop door. Each key opens one machine. Watch the last hook while two people try to start jobs at the same time.

The workshop key rack
The workshop key rack1234567freeSeven machines are busy

Release once after completion.

Structure

GPU admission

GPU admission

Request → Bounded queue (enqueue); Bounded queue → Permit pool (wait); Permit pool → GPU job (admit); GPU job → Completion (finish); Completion → Permit pool (return permit)
Release capacity when the physical work actually ends.

Eight slots on five hosts

Start nine jobs at a barrier. Eight should acquire permits and the ninth should wait or time out. Cancel one admitted job and confirm exactly one waiter proceeds. Repeat against two processes sharing a GPU to expose any accidental per-process quota.

Where a job waits

Who owns the eighth slot?

EventAvailable permitsRunning jobs
Seven jobs are active17
A acquires the final permit08
B asks for a permit08, with B waiting
A completes and releases1 until the next acquisition7
B acquires08

Cancellation while waiting

A client can give up before acquiring a permit, after acquiring one, or while its job continues on a remote GPU. Only the owner of an acquired permit should release it. If client cancellation merely disconnects a listener, the remote job may still be running. Tie release to actual job completion or confirmed termination. Otherwise the ninth job can start while all eight GPUs are still occupied.

Observe both sides of the queue

Measure active jobs, wait time and rejected requests together. Eight busy slots and rising waits indicate sustained demand. Three busy slots and rising waits point elsewhere, such as admission code holding a mutex across a slow network call. A count of available slots alone cannot tell these situations apart.

Own the permit

  • Release once after completion.

Release on disconnect

  • A remote job may still be running.

A cap applied in five processes

5×8=405 \times 8 = 40
If five independent processes each admit eight jobs, up to forty jobs can reach the same physical pool.

Summary

The whole topic in one minute.

A job owns capacity from successful acquisition until the resource becomes available again. Size the waiting room separately and place the limit where all contenders share it.

Sources