lambda fairy

Interruptible sleep in Rust

In my projects, I’ve often wanted to put a thread to sleep for some fixed duration, such that it can be woken up early by another thread.

My first instinct was to raise a Unix signal and catch the resulting EINTR in the receiving thread. But the sleep() function in Rust loops on EINTR by default; and even if it didn’t, this solution still wouldn’t be portable.

The solution I settled on uses std::thread::park_timeout(). This function blocks until another thread unblocks it, or some amount of time has passed, whichever comes first. This behavior is perfect for my use case.

Here’s an example that uses park_timeout() to implement a polling loop. Along with an atomic “keep going” flag, this allows for stopping a worker thread in a controlled way.

(Posted on .)