TechBuzzPro

How to Make Thread Sleep Less Than a Millisecond on Windows

· 6 min read · Windows & PC

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

You cannot reliably make a Windows thread sleep for less than a millisecond using the standard Sleep() function or Thread.Sleep(), because the system timer resolution and scheduler granularity are typically coarser than 1 ms. The practical answer is to avoid sleeping at all: use a spin-wait or hybrid spin-then-yield loop for sub-millisecond delays, or raise the system timer resolution with timeBeginPeriod(1) if you need Sleep(1) to be closer to 1 ms. For true sub-millisecond precision, busy-waiting on a high-resolution counter is the only dependable method on standard Windows PCs.

What You'll Need

  • A Windows PC (Windows 10 or 11; the same APIs exist on older versions).
  • A development environment: Visual Studio with C++ or C#, or any language that can call Win32 APIs (Python with ctypes works too).
  • Basic familiarity with threads and timing functions such as QueryPerformanceCounter and GetTickCount64.
  • Administrator rights are not required for the APIs mentioned here, but some power settings may affect timer behavior.

Step-by-Step

  1. Understand why Sleep() cannot go below 1 ms. The Windows scheduler works in quanta (time slices) that are typically 10–15 ms by default. Sleep(0) yields the remainder of the current quantum, and Sleep(1) asks the scheduler to wake you after at least one clock tick. Even with a 1 ms timer resolution, the actual delay is usually between 1 and 2 ms, and sometimes longer under load. There is no Sleep(0.5) API. So the goal is to replace sleeping with precise waiting.
  2. Choose a busy-wait (spin) approach for the shortest delays. For delays under about 1 ms, spin in a loop checking a high-resolution performance counter. In C/C++, call QueryPerformanceFrequency once to get ticks per second, then QueryPerformanceCounter to get the start time. Compute the target tick count and loop until the counter reaches it. In C#, use Stopwatch.GetTimestamp() and Stopwatch.Frequency. This consumes a CPU core for the duration, which is acceptable only for very short waits (microseconds to a few hundred microseconds).
  3. Use a hybrid strategy for longer but still sub-millisecond targets. If your target is, say, 0.8 ms, spin for the last 0.1–0.2 ms and use a yield or short sleep for the rest. Call SwitchToThread() or Sleep(0) to give up the CPU briefly, then spin for the final stretch. This reduces CPU burn while keeping precision. Do not call Sleep(1) for the bulk of a sub-millisecond wait, because it will overshoot.
  4. Optionally raise the timer resolution. Call timeBeginPeriod(1) at the start of your program (link winmm.lib in C/C++; in C# use P/Invoke) and timeEndPeriod(1) at the end. This makes the system timer tick at 1 ms, which improves the accuracy of Sleep(1) and of waitable timers, but it does not create sub-millisecond sleeps. It can increase power consumption, so use it only while needed.
  5. Consider a waitable timer for near-millisecond precision. CreateWaitableTimerEx with the CREATE_WAITABLE_TIMER_HIGH_RESOLUTION flag (available on Windows 10 1803 and later) allows relative timers with 100-nanosecond units. Set a due time of, for example, 500000 (0.05 ms) and wait on it. In practice, the achievable resolution depends on hardware and load, but it is the closest you get to a sub-millisecond sleep without spinning. Use this when you want the thread to block rather than burn CPU.
  6. Measure and calibrate. Wrap your wait in QueryPerformanceCounter calls and log the actual elapsed time over many iterations. Sub-millisecond waits are affected by CPU frequency scaling, background load, and whether the thread was preempted. If you see occasional multi-millisecond outliers, that is normal on a non-real-time OS. Adjust your spin threshold based on measurements, not assumptions.
  7. Set thread priority if timing is critical. Use SetThreadPriority with THREAD_PRIORITY_TIME_CRITICAL or THREAD_PRIORITY_HIGHEST for the waiting thread, but do so sparingly. High-priority spinning can starve other threads and make the system unresponsive. Pair it with a short spin duration and yield regularly.

Common Mistakes

  • Expecting Sleep(0) to wait a specific amount of time. It does not sleep; it yields the rest of the time slice, which can be 0 ms or up to the quantum length.
  • Calling Sleep(1) and assuming it means exactly 1 ms. On default timer resolution it often means 10–15 ms; even with timeBeginPeriod(1) it is at least 1 ms and usually a bit more.
  • Spinning for long periods (tens of milliseconds) and burning a CPU core. Reserve spinning for the final sub-millisecond portion.
  • Forgetting to call timeEndPeriod(1) after timeBeginPeriod(1), which leaves the system timer at high resolution and increases power draw.
  • Using DateTime.Now or Environment.TickCount for timing. Their resolution is too coarse (often 10–16 ms). Use QueryPerformanceCounter or Stopwatch.
  • Assuming a waitable timer with high-resolution flag guarantees sub-millisecond accuracy under all conditions. It improves resolution but is still subject to scheduling delays.

Tips

  • For delays under ~100 microseconds, a pure spin loop is usually the most accurate and simplest option.
  • Use Sleep(0) or SwitchToThread() inside a spin loop if you are spinning for more than a few hundred microseconds, to avoid hogging the CPU.
  • Test on the target hardware. Virtual machines and laptops with power-saving features often have worse timing behavior than desktops.
  • If you need deterministic sub-millisecond timing for audio or industrial control, consider a real-time OS or a dedicated timing card; Windows is not a real-time operating system.
  • Keep spin durations short and measure the actual delay to tune your threshold.

Troubleshooting

  • Delays are consistently longer than expected: Check that you are not calling Sleep(1) for sub-millisecond targets. Verify timer resolution with a tool or by calling timeBeginPeriod(1). Also check power settings: set the power plan to High Performance and disable CPU throttling if possible.
  • Occasional large overshoots: This is normal preemption. Raise thread priority temporarily or reduce background load. If overshoots are unacceptable, redesign to tolerate jitter or use a real-time solution.
  • High CPU usage: You are spinning too long. Reduce the spin portion and use a waitable timer or Sleep(0) for the bulk of the delay.
  • timeBeginPeriod has no effect: Ensure you link the correct library (winmm.lib) and call it before creating timing-sensitive threads. On some systems, other processes may already have raised the timer resolution.
  • Waitable timer with high-resolution flag fails: The flag requires Windows 10 1803 or later. Check the OS version and fall back to a spin loop if CreateWaitableTimerEx returns NULL.

Frequently Asked Questions

Can I call Sleep(0.5) in C++ or C#?

No. Sleep and Thread.Sleep take integer milliseconds. There is no fractional-millisecond sleep API. Use a spin loop or a high-resolution waitable timer instead.

Does timeBeginPeriod(1) make Sleep(1) exactly 1 ms?

No. It sets the system timer to 1 ms resolution, so Sleep(1) will wait at least 1 ms, but actual wake-up can be slightly later due to scheduling. It does not enable sub-millisecond sleeps.

Is busy-waiting bad for my CPU?

It consumes CPU cycles for the duration of the wait. For very short waits (microseconds) it is fine. For longer waits, use a hybrid approach or a waitable timer to avoid wasting power and slowing other work.

What is the most precise way to wait under 1 ms on Windows?

A spin loop on QueryPerformanceCounter is the most precise. A high-resolution waitable timer is the best blocking alternative, but it still depends on system load and hardware.

Why does my thread sometimes wake up several milliseconds late?

Windows is not a real-time OS. The scheduler may preempt your thread, and timer resolution plus power management can add latency. Measure and design for jitter, or use a real-time platform if strict guarantees are required.

Can I use Python to do sub-millisecond sleeps on Windows?

Python's time.sleep() has the same millisecond granularity limits. You can call QueryPerformanceCounter via ctypes for a spin loop, or use time.perf_counter() in a busy-wait. The same trade-offs apply.