Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When we create and start a new .NET thread, does it create a new OS level thread?

When we create a new thread in this way:

Threading.Thread t = new Threading.Thread(() => { Console.WriteLine("My New thread"); });
t.Start();

Is it going to create an OS level thread as well?

like image 368
Randeep Singh Avatar asked May 23 '15 13:05

Randeep Singh


1 Answers

Technically this is undefined, a custom CLR host can use any construct to implement a thread. The underlying hosting interface is IClrTask. Nor is there a decent way to find out.

Practically this never happens. IClrTask was added at the request of the SQL Server group which wanted the option to map a thread onto a fiber. That project ultimately failed, they could not get it stable enough. A red flag to anybody that might have had similar plans. Unless you are acting as a plugin to a large unmanaged program similar to SQL Server, you can always assume you'll consume an OS thread.

like image 182
Hans Passant Avatar answered Oct 25 '22 02:10

Hans Passant