Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a suspended thread?

The Thread.Abort() documentation states that it will throw ThreadStateException if:

The thread that is being aborted is currently suspended.

What circumstance is this exactly?

If, for example, the thread is in the middle of a Thread.Sleep(1000), or waiting on a WaitHandle, is it considered "suspended"?

like image 830
Qwertie Avatar asked Dec 27 '22 21:12

Qwertie


1 Answers

From reading the documentation, it is my interpretation that "suspended" in this context does not relate to the use of Thread.Sleep or WaitHandle usage, but instead refers to the thread state encountered when using the obsoleted Thread.Suspend or equivalent Windows API SuspendThread.

In this context, a suspended thread is a thread whose execution has been explicitly paused (or suspended), i.e. it's context is no longer being executed but has been suspended to be resumed at some later point.

This technique is primarily used by debuggers and is not recommended for sychronization activities. This is clarified in the MSDN documentation on SuspendThread:

This function is primarily designed for use by debuggers. It is not intended to be used for thread synchronization. Calling SuspendThread on a thread that owns a synchronization object, such as a mutex or critical section, can lead to a deadlock if the calling thread tries to obtain a synchronization object owned by a suspended thread.

like image 179
Jeff Yates Avatar answered Jan 11 '23 17:01

Jeff Yates