Why is it bad for pausing / interrupting threads?

My ThreadManager model is that some ThreadManager give each thread a queue. When it turns into a stream, it receives a few lines of code.

To pause a thread, could not just the ThreadManager (instantly) stop it so that the thread has a turn?

To interrupt a thread, could ThreadManager simply not give this thread another turn?

What is the problem?

+7
source share
2 answers

Quote from MSDN about thread suspension:

You have no way of knowing what code the thread executes when you suspend this. If you pause a thread while it keeps blocking during the security assessment of permissions, other topics in AppDomain may be blocked. If you pause a thread while it is executing a class constructor, other threads in AppDomain that try to use this class are blocked. Dead ends can occur very easily.

A repaired thread can lead to unpredictable circumstances. There is a good article about this: http://www.bluebytesoftware.com/blog/2009/03/13/ManagedCodeAndAsynchronousExceptionHardening.aspx

+12
source

I agree with Alex, but for further clarification, if you need to "pause" the stream, it will probably be better to look at some kind of locking mechanism, such as semaphores, mutexes, or one of many others available.

But without knowing your code, Windows is a proactive multi-tasking environment. Usually this is not necessary, just let your threads execute and the main OS and scheduler will keep track of all your tasks.

+4
source

All Articles