Change Instant in Mutex

I want to print progress info in regular intervals while a rayon iterator runs. This is what I have so far:

let counter = AtomicU32::new(1);
let total = downloads.len();
let mut last_print = Mutex::new(Instant::now());

downloads.into_par_iter().map(|download: String| {
	let download_no = counter.fetch_add(1, Ordering::SeqCst);
	{
		let lp = last_print.lock().unwrap();
		if lp.elapsed() > Duration::from_secs(5) {
			eprintln!("currently at download {} of {}", download_no, total);
		}
	}

	// do the download

});

So, 5 seconds after the start this will print for every download. I need to update last_print to the current time to prevent that. How can I do that?

You declare your mutex guard as mutable and dereference it:

let mut lp = last_print.lock().unwrap();
*lp = Instant::now();

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.