[Tutor] stopping threads ?

Lie Ryan lie.1296 at gmail.com
Mon Nov 3 18:14:35 CET 2008


On Mon, 03 Nov 2008 14:46:09 +0000, dave selby wrote:

> Hi All,
> 
> Why when I use threads in my app (I know they are evil ...lol) does it
> not stop with ctrl-c, I have to use ctrl-z ?
> 
> Cheers
> 
> Dave

Wonder why? Because Ctrl-C merely raises KeyboardInterrupt in the main 
thread, breaking the main thread, while leaving the other threads 
running. To interrupt a multi-threaded program, you'd need to stop all 
running threads. Stopping the current thread is usually easy if you write 
that thread code, it's usually merely breaking the loop or raising an 
unhandled exception (or an exception handled outside the thread loop). 
The problem is to communicate to all threads to quit immediately, a 
common way is by using an event queue, when the main thread receives a 
Keyboard Interrupt, it sends as many Quit Event as the number of threads. 
This Quit Event would be caught by each thread and each thread would 
exits upon receiving the Quit Event.



More information about the Tutor mailing list