为什么需要多线程
對于這個問題可能很多朋友會說是為了高性能,個人覺得這是誤解,多線程不等于高性能,從cpu(單核)的角度上看單線程才能帶來最高性能。
對于單純的運算任務來說一條線程依次執行到底肯定是最快速的(因為線程間的調度,通信及資源的共享等都需要額外的開銷),在計算機的早期歲月,操作系統沒有提供線程概念。事實上整個只運行著一個執行線程,?其中同時包含操作系統代碼和應用程序代碼。當然這樣帶來的問題也很明顯一旦應用程序出現問題只能reset了。不過我還是認為多線程產生是因為CPU太快了,從計算機誕生CPU運算速度的提升實在是太快了,當然不是說因為快就要分幾個線程出去運行。計算機的運行絕不只是中央處理器CPU一個人的事情,還要數據的儲存,傳輸(數據加算法嘛,算法靠cpu,數據靠存儲器了),數據的存取遠遠跟不上了,當然后面還出現了互聯網,網絡的傳輸速度更無法與cpu相匹配。
所以個人看法是多線程的出現是科技進步的必然結果,試想一下應用程序得出了目標結果不過卻存不進去或傳不出去,那是多么的著急,(當然多線程還帶了跟多的好處,如應用程序的隔離等)而事實上多線程的應用場景多是什么地方,什么地方必須使用到多線程這些都十分有規律,那些一定會阻塞的操作大部分會使用到多線程,而他們為什么會阻塞,我們可以發現這些操作一般都是磁盤的讀取,網絡請求的處理,這些操作受制于環境必須阻塞,而我們不可能一直等在那里。
?
然后對于線程跟性能,我想簡單談一下自己的看法,多線程絕對不能與高性能劃等號,而多線程的額外開銷也沒有達到用戶能察覺的地步(即使很多權威的書籍中強掉線程的切換回帶來十分大的性能耗費,而實際的經驗都表明這消耗是十分難以擦覺的,個人認為書中的出發點可能不同,對于cpu本身在加上現在windows動不動就2,3千條的線程也是是一個不應該被忽視的內容)
?
然后還是先講下線程的開銷吧
?
?
這些都是線程應該具備的一些東西(windows),也就是說創建一個線程就需要創建這樣東西,剛剛我說線程切換很難被察覺,可不是線程創建,創建大量的線程的確是需要大量的時間(所以高級的程序框架會提供線程池或類似的東西,緩解線程頻繁創建銷毀帶來的負面影響)
?
再談下線程的切換
?
然后時候操作系(windows)都只將一個線程分配給一個cpu(其實就是任何時候一個cpu都只能處理一個線程),線程允許運行一個”時間片“一旦時間片到期windows就會進行切換(對此還必須提一下windows可能會在時間片運行中的任何時候進行切換,同時時間片到期后也是可能選擇同一個線程進行特殊的切換的)
讓我們看看切換的時候都要完成什么
?
?
一般情況下”時間片“大約為30毫秒,雖然書上是這樣寫的,不過實際測試結果遠小于這個時間,可能是書寫的比較早吧,一般情況下這個時間片會根據操作系統的運行負擔進行自動調節。
當然時間片里的時間畢竟都是應用程序本身的消耗,所以這些消耗都是有意義的,而對于線程切換則完全由系統跟cpu內部完成,對于應用程序業務本身來說的確是浪費。
好在如上面提到的實際上的消耗很難被察覺,一般情況下甚至很難被準確的測試出確切的時間數據,因為消耗的時間不能以我們常見的毫秒甚至是微秒來衡量,實際的測試只能說明一次切換的消耗在個人計算機上一定是納米級別的
?
Context Switch DefinitionA context switch (also sometimes referred to as a process switch or a task switch) is the switching of the CPU (central processing unit) from one process or thread to another.A process (also sometimes referred to as a task) is an executing (i.e., running) instance of a program. In Linux, threads are lightweight processes that can run in parallel and share an address space (i.e., a range of memory locations) and other resources with their parent processes (i.e., the processes that created them).A context is the contents of a CPU's registers and program counter at any point in time. A register is a small amount of very fast memory inside of a CPU (as opposed to the slower RAM main memory outside of the CPU) that is used to speed the execution of computer programs by providing quick access to commonly used values, generally those in the midst of a calculation. A program counter is a specialized register that indicates the position of the CPU in its instruction sequence and which holds either the address of the instruction being executed or the address of the next instruction to be executed, depending on the specific system.Context switching can be described in slightly more detail as the kernel (i.e., the core of the operating system) performing the following activities with regard to processes (including threads) on the CPU: (1) suspending the progression of one process and storing the CPU's state (i.e., the context) for that process somewhere in memory, (2) retrieving the context of the next process from memory and restoring it in the CPU's registers and (3) returning to the location indicated by the program counter (i.e., returning to the line of code at which the process was interrupted) in order to resume the process.A context switch is sometimes described as the kernel suspending execution of one process on the CPU and resuming execution of some other process that had previously been suspended. Although this wording can help clarify the concept, it can be confusing in itself because a process is, by definition, an executing instance of a program. Thus the wording suspending progression of a process might be preferable.Context Switches and Mode SwitchesContext switches can occur only in kernel mode. Kernel mode is a privileged mode of the CPU in which only the kernel runs and which provides access to all memory locations and all other system resources. Other programs, including applications, initially operate in user mode, but they can run portions of the kernel code via system calls. A system call is a request in a Unix-like operating system by an active process (i.e., a process currently progressing in the CPU) for a service performed by the kernel, such as input/output (I/O) or process creation (i.e., creation of a new process). I/O can be defined as any movement of information to or from the combination of the CPU and main memory (i.e. RAM), that is, communication between this combination and the computer's users (e.g., via the keyboard or mouse), its storage devices (e.g., disk or tape drives), or other computers.The existence of these two modes in Unix-like operating systems means that a similar, but simpler, operation is necessary when a system call causes the CPU to shift to kernel mode. This is referred to as a mode switch rather than a context switch, because it does not change the current process.Context switching is an essential feature of multitasking operating systems. A multitasking operating system is one in which multiple processes execute on a single CPU seemingly simultaneously and without interfering with each other. This illusion of concurrency is achieved by means of context switches that are occurring in rapid succession (tens or hundreds of times per second). These context switches occur as a result of processes voluntarily relinquishing their time in the CPU or as a result of the scheduler making the switch when a process has used up its CPU time slice.A context switch can also occur as a result of a hardware interrupt, which is a signal from a hardware device (such as a keyboard, mouse, modem or system clock) to the kernel that an event (e.g., a key press, mouse movement or arrival of data from a network connection) has occurred.Intel 80386 and higher CPUs contain hardware support for context switches. However, most modern operating systems perform software context switching, which can be used on any CPU, rather than hardware context switching in an attempt to obtain improved performance. Software context switching was first implemented in Linux for Intel-compatible processors with the 2.4 kernel.One major advantage claimed for software context switching is that, whereas the hardware mechanism saves almost all of the CPU state, software can be more selective and save only that portion that actually needs to be saved and reloaded. However, there is some question as to how important this really is in increasing the efficiency of context switching. Its advocates also claim that software context switching allows for the possibility of improving the switching code, thereby further enhancing efficiency, and that it permits better control over the validity of the data that is being loaded.The Cost of Context SwitchingContext switching is generally computationally intensive. That is, it requires considerable processor time, which can be on the order of nanoseconds for each of the tens or hundreds of switches per second. Thus, context switching represents a substantial cost to the system in terms of CPU time and can, in fact, be the most costly operation on an operating system.Consequently, a major focus in the design of operating systems has been to avoid unnecessary context switching to the extent possible. However, this has not been easy to accomplish in practice. In fact, although the cost of context switching has been declining when measured in terms of the absolute amount of CPU time consumed, this appears to be due mainly to increases in CPU clock speeds rather than to improvements in the efficiency of context switching itself.One of the many advantages claimed for Linux as compared with other operating systems, including some other Unix-like systems, is its extremely low cost of context switching and mode switching.?
這里提供一個很權威的描述,就是上面的
講的是上下文切換也就是線程間的切換,是Linux的,不過線程的處理都是十分相似的。
which?can?be?on?the?order?of?nanoseconds?for?each?of?the?tens?or?hundreds?of?switches?per?second 。 我英文不是很好,大致是說每秒幾百或幾十次的切換,而實際上只消耗了幾納秒。當然現如今的windows的切換次數會大的多 借助工具我們可以看到線程切換的次數十分驚人,單qq這個進程的所有活著的線程一共切換了3億多次,這個數量級帶來的性能消耗可能就不應該被直接忽視掉。 不過事實上實際經驗發現,由多線程帶來的性能消耗或程序業務處理能力的下降都不是由這正常的切換造成的,事實上線程的濫用是導致性能下降直接原因,而這些濫用是普遍存在的,在不該使用多線程的時候使用了它,頻繁的創建及銷毀線程,不正確的使用線程鎖,讓線程頻繁訪問共享資源等等不合理操作導致多線程對資源或性能的消耗比系統切換來說,頁面的切換也就不止一提了轉載于:https://www.cnblogs.com/lulianqi/p/4808398.html
總結
- 上一篇: HDU 5439 Aggregated
- 下一篇: make and make bzIma