25.6 CLR threads and Windows threads. CLRThread andWindowsThread
Today, CLR uses the thread processing capability of windows. Therefore, part V of this chapter focuses on the thread processing capability exposed by windows to developers. I will explain how the thread works in windows and how the CLR changes the thread behavior (if possible ). However, if you want to learn more about threads, you are advised to read some of my previous books on this topic, such as my book windows via C/C ++ (Microsoft press, 2007 ).
Although a CLR thread today is mapped directly to a Windows Thread, the Microsoft CLR team reserves the right to detach it from a Windows Thread in the future. One day, CLR may introduce its own logic thread concept, so that a CLR logic thread is not necessarily mapped to a physical Windows Thread. It is said that logical threads will use much less resources than physical threads, so they can run a large number of logical threads on a very small number of physical threads. For example, CLR can determine that one of your threads is in the waiting state and re-allocate that thread to perform a different task. The benefits of this solution are: Simpler coding, fewer resources used, and potential performance improvement. Unfortunately, the CLR team still has a lot of work to implement this solution, so it is unlikely that the CLR will launch this function in the near future.
for you, all this means that when manipulating a thread, Code should make as few assumptions as possible. For example, P/invoke should be avoided for Windows functions because these functions do not know anything about CLR threads. By avoiding the use of local Windows functions and sticking to the FCL (framework class library) type, your code will be able to enjoy this improvement immediately after the performance is improved.