title: How does thread pool manage work item with worker thread when one task is blocking?
body: <p>My question is based on this article <a href="https://www.codeproject.com/Articles/662735/Internals-of-Windows-Thread" rel="nofollow noreferrer">Internals of Windows Thread</a></p>
  <p>We can see that for every thread in system, operating system create one thread kernel object. Operating systems use these thread kernel objects for managing and executing threads across the system</p>
  <p><a href="https://i.stack.imgur.com/v5JUU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/v5JUU.png" alt="enter image description here" /></a></p>
  <p>Every 20 milliseconds or so, operating system thread scheduler looks at all the thread kernel objects currently inside Ready Queue (doubly linked list). Thread scheduler selects one of the thread kernel objects and loads the CPU's registers with the values that were last saved in the thread's context.</p>
  <p><a href="https://i.stack.imgur.com/0Dp1C.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0Dp1C.png" alt="enter image description here" /></a></p>
  <p>And CLR's thread pool architecture is</p>
  <p><a href="https://i.stack.imgur.com/bv5y6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bv5y6.png" alt="enter image description here" /></a></p>
  <p>Each worker thread has its own local queue, and when a worker thread schedules a Task, the Task is added to calling the thread's local queue.</p>
  <p>With this background, let's say .NET ThreadPool has some worker hreads as picture above shows. Let's say Worker Thread 1 has some worker items queueing in the Local Queue 1. The current task the Worker Thread 1 executing is blocking for a couple of seconds(wait for a signal to arrive etc or it is executing a long running task).</p>
  <p>My question is,</p>
  <p><strong>A</strong>.can Worker Thread 1 temporarily stop executing this blocking task and switch to execute another task in its local queue, then after the new task finishes and then come back to execute the long running task again. If this is the case, how can the long running task's context state be saved?for example, we need to store the registers' values into the Thread Kernel Object 1, but when Worker Thread 1 starts to execute another task, then the original task's state saved in the Thread Kernel Object 1 will get lost</p>
  <p>or</p>
  <p><strong>B</strong>.Worker Thread 1 has to finish this long running task first then it can execute other task in its local queue. But this is inefficient as Worker Thread 1 wastes a couple of seconds that it could have used to execute other task.</p>