Comparison of thread::park_timeout() and thread::sleep()

Currently reading Mara Bos's excellent book Rust Atomics and Locks. In the first chapter, I came across, thread::park() and thread::park_timeout(). A quick comparison (GPT) tells me that the former does not consumer CPU resources while in the state of 'suspended animation', which instantly makes it slightly if not outright better than thread::sleep(). and with thread::park_timeout(), we also do away with the need to use another thread to unpark the thread. With this conclusion, why should I not do away with thread::sleep() altogether in my codebase ?

A thread does not consume CPU resources while in a call to thread::sleep.


Don't trust GPT, it is usually a liar.


duped by GPT again. moving on though, does that mean that thread::park_timeout() and thread::sleep() have essentially, the same effect ?

park_timeout allows the thread to be woken early by another thread.


You can see the differences by reading the documentation.


Blocks unless or until the current thread’s token is made available or the specified duration has been reached (may wake spuriously).

The semantics of this function are equivalent to park except that the thread will be blocked for roughly no longer than dur. This method should not be used for precise timing due to anomalies such as preemption or platform differences that might not cause the maximum amount of time waited to be precisely dur long.


Puts the current thread to sleep for at least the specified amount of time.

The thread may sleep longer than the duration specified due to scheduling specifics or platform-dependent functionality. It will never sleep less.

So there's some differences.

  • park_timeout will wake if the thread is unparked ("token is made available")
  • park_timeout may wake spuriously, while sleep will always sleep at least as long as the duration
  • park_timeout only claims to have rough timing capabilities on either side of the duration, while sleep will always be at least the duration

So you could write a sleep function yourself using park_timeout, but not the other way around.


thanks for breaking it down so nicely!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.