A busy-waiting loop, also called a busy loop or spin loop, waits by repeatedly checking elapsed time instead of suspending the thread with the operating system. In Rust, the loop can call thread::yield_now() while it waits. section 10.2.5
Use the technique when short, timing-sensitive pauses matter, but account for its costs:
- Improve short-wait timing compared with sleep, which is not perfectly accurate. section 10.2.4
- Combine sleep for most of a delay with spinning near the end when appropriate. section 10.2.4
- Avoid assuming that CPU-intensive threading scales beyond the number of physical cores; performance degrades quickly after that point. section 10.2.4
A 20-millisecond spin loop
Record the start time, set a 20-millisecond duration, and repeatedly yield the current thread until the duration has elapsed: while start.elapsed() < pause { thread::yield_now(); }. Spawn this work in threads, then remove and join each handler with while let Some(handle) = handlers.pop().


