Interrupt a thread

Is there any way i can interrupt a thread (spawned by thread::Builder::spawn()) when it is sleeping (with thread::sleep()) ? sleep() would be a good interruption point, so i want this.

There would be many things i would like to do with that capability (eg., making of a cancelable Timer etc.)

I don't know of a way. However, I would probably build this functionality on top of a std::sync::Condvar - instead of sleeping, try to acquire a condvar for that thread with a timeout for that amount of time, and you can "wake" a thread by notifying the condvar.

1 Like