Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread lifecycle in .NET framework

The state of a thread in .NET framework is explained in this link.

I recently saw this picture in a web-site and a couple of questions came to my mind:

enter image description here

  1. The thread lifecycle in the OS is not completely aligned with thread lifecycle in .NET framework. Can someone provide a resource that matches the states in OS with .NET framework?

  2. We don't have a state called Blocked in .NET framework. What will be the state of a thread if it issues an I/O request?

  3. What is the purpose of the Aborted state? When a thread calls the Abort() method, it will go to the AbortRequested state and after the thread responds to abort request, it will go to Stopped state. So what is the function of Aborted state?

like image 471
ManiAm Avatar asked Jan 23 '12 02:01

ManiAm


People also ask

What is thread life cycle in C#?

The life cycle of a thread is started when instance of System. Threading. Thread class is created. When the task execution of the thread is completed, its life cycle is ended.

What is the lifecycle of thread?

A thread goes through various stages in its lifecycle. For example, a thread is born, started, runs, and then dies. The following diagram shows the complete life cycle of a thread. New − A new thread begins its life cycle in the new state.

What is a thread in .NET framework?

Threads are tasks that can run concurrently to other threads and can share data. When your program starts, it creates a thread for the entry point of your program, usually a Main function.

What are the different types of threads in C#?

There are two types of threads, foreground and background. Besides the main application thread, all threads created by calling a Thread class constructor are foreground threads. Background threads are the threads that are created and used from the ThreadPool, which is a pool of worker threads maintained by the runtime.


2 Answers

A thread is blocked if its execution has been suspended by one of the synchronization primitives, such as a lock or a mutex. Any thread that is performing useful work at a given moment is, by definition, not blocked at that moment.

The AbortRequested/Stopped cycle gives the thread an opportunity to perform an orderly shutdown, releasing acquired resources and performing other cleanup tasks.

http://www.albahari.com/threading/

like image 81
Robert Harvey Avatar answered Sep 20 '22 09:09

Robert Harvey


Answers to your questions:

  1. I don't believe this mapping would be as useful as you appear to hope. I've never run across one and never needed it.
  2. There isn't a real need for a "Blocked" state unless you're trying to write something like a deadlock detector (rather advanced). From a typical developer perspective, the OS "blocked" state is transient and can be ignored. (It appears that your code is running, but the OS has nothing to do until the async response is received.)
  3. Imagine the Aborted state as .NET providing an exception handler around all the code in the thread. When an exception is caught, causing the thread to die, .NET translates that to an Aborted state for you. Otherwise, you may not be able to tell the difference between abnormal and normal thread termination.
like image 23
John Fisher Avatar answered Sep 20 '22 09:09

John Fisher