Pausing thread execution with timers
Sometimes, during the processing of a thread, there may be a need to pause execution either to wait for another event or to synchronize execution with other threads. Rust provides support for this using the std::thread::sleep
function. This function takes a time duration of type time::Duration
and pauses execution of the thread for the specified time. During this time, the processor time can be made available to other threads or applications running on the computer system. Let's see an example of the usage of thread::sleep
:
use std::thread; use std::time::Duration; fn main() { let duration = Duration::new(1,0); println!("Going to sleep"); thread::sleep(duration); println!("Woke up"); }
Using the sleep()
function is fairly straightforward, but this blocks the current thread and it is important to make judicious use of this...