1、计算机线程 外文翻译 外文文献 英文文献译 文Chapter 25 Thread BasicsWhy Does Windows Support Threads? .691Thread Overhead. 692Stop the Madness.696CPU Trends.699NUMA Architecture Machines.700CLR Threads and Windows Threads.703Using a Dedicated Thread to Perform an AsynchronousCompute-Bound Operation.704Reasons to Use Thr
2、eads.706Thread Scheduling and Priorities.708Foreground Threads versus Background Threads.713What Now?. .715In this chapter, I introduce the basic concepts concerning threads, and I offer a way for developers to conceptualize about them and their use. Ill explain why Microsoft Windows introduced the
3、concept of threads, CPU trends, the relationship between common language runtime (CLR) threads and Windows threads, the overhead associated with using threads ,how Windows schedules threads, the Microsoft .NET Framework classes that expose thread properties, and much more.The chapters in Part V of t
4、his book, “Threading,” explain how Windows and the CLR work together to provide a threading architecture. It is my hope that after reading these chapters,you will take away a foundation of knowledge that will allow you to effectively use threads to design and build responsive, reliable, and scalable
5、 applications and components.Why Does Windows Support Threads?Back in the early days of computers, operating systems didnt offer the concept of a thread.In effect, there was just one thread of execution that ran throughout the entire system, which included both operating system code and application
6、code. The problem with having only one thread of execution was that a long-running task would prevent other tasks from executing.For example, in the days of 16-bit Windows, it was very common for an application that was printing a document to stall the entire machine, causing the OS and all other ap
7、plications to stop responding.And, sometimes applications would have a bug in them, resulting in an infinite loop that also stopped the entire machine from operating.At this point, the end user would have no choice but to reboot the computer by pressing the reset button or power switch. Of course, e
8、nd users hated doing this (they still do, in fact) because all running applications terminated; more importantly, any data that these applications were processing was thrown out of memory and lost. Microsoft knew that 16-bit Windows would not be a good enough operating system to keep Microsoft relev
9、antas the computer industry progressed, so they set out to build a new OS to address the needs of corporations and individuals. This new OS had to be robust, reliable, scalable, and secure, and it had to improve the many deficiencies of 16-bit Windows. This OS kernel originally shipped in Microsoft
10、Windows NT. Over the years, this kernel has had many tweaks and features added to it. The latest version of this kernel ships in the latest versions of the Microsoft client and server Windows operating systems.When Microsoft was designing this OS kernel, they decided to run each instance of an appli
11、cation in what is called a process. A process is just a collection of resources that is used by a single instance of an application. Each process is given a virtual address space, ensuring that the code and data used by one process is not accessible to another process. This makes application instanc
12、es robust because one process cannot corrupt code or data being used by another. In addition, the OSs kernel code and data are not accessible to processes; therefore,its not possible for application code to corrupt operating system code or data. So now,application code cannot corrupt other applicati
13、ons or the OS itself, and the whole computing experience is much better for end users. In addition, the system is more secure because application code cannot access user names, passwords, credit card information, or other sensitive information that is in use by another application or the operating s
14、ystem itself.This is all well and good, but what about the CPU itself? What if an application enters an infinite loop? Well, if there is only one CPU in the machine, then it executes the infinite loop and cannot execute anything else, so while the data cannot be corrupted and is more secure,the syst
15、em could still stop responding to the end user. Microsoft needed to fix this problem,too, and threads were the answer. A thread is a Windows concept whose job is to virtualize the CPU. Windows gives each process its very own thread (which functions similar to a CPU),and if application code enters an
16、 infinite loop, the process associated with that code freezesup, but other processes (which have their own threads) are not frozen; they keep running!Thread OverheadThreads are awesome because they enable Windows to be responsive even when applicationsare executing long-running tasks. Also, threads
17、allow the user to use one application(like Task Manager) to forcibly kill an application that appears frozen because it is executing a long-running task. But as with every virtualization mechanism, threads have space (memory consumption) and time (runtime execution performance) overhead associated w
18、ith them.Lets explore this overhead in more detail now. Every thread has one of each of the following: Thread kernel object The OS allocates and initializes one of these data structures for each thread created in the system. The data structure contains a bunch of properties(discussed later in this c
19、hapter) that describe the thread. This data structure also contains what is called the threads context. The context is a block of memory that contains a set of the CPUs registers. When Windows is running on a machine with an x86 CPU, the threads context uses about 700 bytes of memory. For x64 and IA
20、64 CPUs, the context is about 1,240 and 2,500 bytes of memory, respectively. Thread environment block (TEB) The TEB is a block of memory allocated and initialized in user mode (address space that application code can quickly access). The TEB consumes 1 page of memory (4 KB on x86 and x64 CPUs, 8 KB
21、on an IA64 CPU).The TEB contains the head of the threads exception-handling chain. Each try block that the thread enters inserts a node in the head of this chain; the node is removed from the chain when the thread exists in the try block. In addition, the TEB contains the threads thread-local storag
22、e data as well as some data structures for use by Graphics Device Interface (GDI) and OpenGL graphics. User-mode stack The user-mode stack is used for local variables and arguments passed to methods. It also contains the address indicating what the thread should execute next when the current method
23、returns. By default, Windows allocates 1 MB of memory for each threads user-mode stack. Kernel-mode stack The kernel-mode stack is also used when application code passes arguments to a kernel-mode function in the operating system. For security reasons,Windows copies any arguments passed from user-mo
24、de code to the kernel from the threads user-mode stack to the threads kernel-mode stack. Once copied, the kernel can verify the arguments values, and since the application code cant access the kernelmode stack, the application cant modify the arguments values after they have been validated and the O
25、S kernel code begins to operate on them. In addition, the kernel calls methods within itself and uses the kernel-mode stack to pass its own arguments,to store a functions local variables, and to store return addresses. The kernel-mode stack is 12 KB when running on a 32-bit Windows system and 24 KB
26、when running on a 64-bit Windows system. DLL thread-attach and thread-detach notifications Windows has a policy that whenever a thread is created in a process, all DLLs loaded in that process have their DllMain method called, passing a DLL_THREAD_ATTACH flag. Similarly, whenever a thread dies, all D
27、LLs in the process have their DllMain method called, passing it a DLL_THREAD_DETACH flag. Some DLLs need these notifications to perform some special initialization or cleanup for each thread created/destroyed in the process. For example,the C-Runtime library DLL allocates some thread-local storage s
28、tate that is required should the thread use functions contained within the C-Runtime library.In the early days of Windows, many processes had maybe 5 or 6 DLLs loaded into them,but today, some processes have several hundred DLLs loaded into them. Right now, on mymachine, Microsoft Office Outlook has
29、 about 250 DLLs loaded into its process address space!This means that whenever a new thread is created in Office Outlook, 250 DLL functions must get called before the thread is allowed to do what it was created to do. And these 250 functions must be called again whenever a thread in Outlook dies. Wo
30、wthis can seriously affect the performance of creating and destroying threads within a process.So now, you see all the space and time overhead that is associated with creating a thread, letting it sit around in the system, and destroying it. But the situation gets even worsenowwere going to start ta
31、lking about context switching. A computer with only one CPU in it can do only one thing at a time. Therefore, Windows has to share the actual CPU hardware among all the threads (logical CPUs) that are sitting around in the system.At any given moment in time, Windows assigns one thread to a CPU. That thread is allowed to run for a time-slice (sometimes referred to as a quantum). When the time-slice expires,Windows context switches to another thread. Every context switch requires that Windows performs the f
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1