Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are .NET threads different from operating system threads?

Tags:

  1. Are .NET threads lightweight user-mode threads or are they kernel-mode operating system threads?

  2. Also, sparing SQL Server, is there a one-to-one correspondence between a .NET thread an an operating system thread?

I am also intrigued because the Thread class that has a symmetric pair of methods named BeginThreadAffinity and EndThreadAffinity, whose documentation subtly suggests that .NET threads are lightweight abstractions over real operating system threads.

Also, I read a while ago on some stack overflow thread itself that Microsoft scratched an attempt to maintain this separation in the CLR, like SQL Server does. There was some project underway to use the Fiber API for this purpose, I recollect, but I can't say I understood all the details of what I read.

I would like some more detailed literature on this topic such as the internal structure of a .NET thread vis-a-vis that of a thread created by Windows. While there is plenty of information available on the structure of a thread created by Windows, Jeffrey Richter's Advanced Windows Programming book being one of the sources, for instance, I can't find any literature dedicated to the internal structure of a .NET thread.

One might argue that this information is available in the .NET source code, which is now publicly available, or using a disassembler such as Reflector or IL Spy, but I don't see anything to represent the Thread Control Block (TCB) and Program Counter (PC) and Stack Pointer (SP) or the thread's wait queue, or the list of queues that the thread is currently a member of in the Thread class.

Where can I read about this? Does the documentation mention any of it? I have read all of these pages from the MSDN but they do not seem to mention it.

like image 534
Water Cooler v2 Avatar asked May 27 '16 19:05

Water Cooler v2


People also ask

What is a .NET thread?

Net Threading. Thread in computer science means a sequence of execution instructions that can run independently , that is a single flow of execution in a process. Thread is like a process, at least one thread exists within each process.

What are the different types of threads in operating system?

There are two types of threads to be managed in a modern system: User threads and kernel threads.

Is .NET single threaded?

NET program is started with a single thread, often called the primary thread. However, it can create additional threads to execute code in parallel or concurrently with the primary thread.

What is thread in .NET core?

Thread is a foreground thread. You have tasks that cause the thread to block for long periods of time. The thread pool has a maximum number of threads, so a large number of blocked thread pool threads might prevent tasks from starting. You need to place threads into a single-threaded apartment.


1 Answers

.NET's threads are indeed abstractions, but you can basically think of them as nearly identical to OS threads. There are some key differences especially with respect to garbage collection, but to the vast majority of programmers (read: programmers who are unlikely to spin up WinDBG) there is no functional difference.

For more detail, read this

like image 62
Andy Hopper Avatar answered Sep 29 '22 21:09

Andy Hopper