为什么需要多线程
對(duì)于這個(gè)問(wèn)題可能很多朋友會(huì)說(shuō)是為了高性能,個(gè)人覺得這是誤解,多線程不等于高性能,從cpu(單核)的角度上看單線程才能帶來(lái)最高性能。
對(duì)于單純的運(yùn)算任務(wù)來(lái)說(shuō)一條線程依次執(zhí)行到底肯定是最快速的(因?yàn)榫€程間的調(diào)度,通信及資源的共享等都需要額外的開銷),在計(jì)算機(jī)的早期歲月,操作系統(tǒng)沒有提供線程概念。事實(shí)上整個(gè)只運(yùn)行著一個(gè)執(zhí)行線程,?其中同時(shí)包含操作系統(tǒng)代碼和應(yīng)用程序代碼。當(dāng)然這樣帶來(lái)的問(wèn)題也很明顯一旦應(yīng)用程序出現(xiàn)問(wèn)題只能reset了。不過(guò)我還是認(rèn)為多線程產(chǎn)生是因?yàn)镃PU太快了,從計(jì)算機(jī)誕生CPU運(yùn)算速度的提升實(shí)在是太快了,當(dāng)然不是說(shuō)因?yàn)榭炀鸵謳讉€(gè)線程出去運(yùn)行。計(jì)算機(jī)的運(yùn)行絕不只是中央處理器CPU一個(gè)人的事情,還要數(shù)據(jù)的儲(chǔ)存,傳輸(數(shù)據(jù)加算法嘛,算法靠cpu,數(shù)據(jù)靠存儲(chǔ)器了),數(shù)據(jù)的存取遠(yuǎn)遠(yuǎn)跟不上了,當(dāng)然后面還出現(xiàn)了互聯(lián)網(wǎng),網(wǎng)絡(luò)的傳輸速度更無(wú)法與cpu相匹配。
所以個(gè)人看法是多線程的出現(xiàn)是科技進(jìn)步的必然結(jié)果,試想一下應(yīng)用程序得出了目標(biāo)結(jié)果不過(guò)卻存不進(jìn)去或傳不出去,那是多么的著急,(當(dāng)然多線程還帶了跟多的好處,如應(yīng)用程序的隔離等)而事實(shí)上多線程的應(yīng)用場(chǎng)景多是什么地方,什么地方必須使用到多線程這些都十分有規(guī)律,那些一定會(huì)阻塞的操作大部分會(huì)使用到多線程,而他們?yōu)槭裁磿?huì)阻塞,我們可以發(fā)現(xiàn)這些操作一般都是磁盤的讀取,網(wǎng)絡(luò)請(qǐng)求的處理,這些操作受制于環(huán)境必須阻塞,而我們不可能一直等在那里。
?
然后對(duì)于線程跟性能,我想簡(jiǎn)單談一下自己的看法,多線程絕對(duì)不能與高性能劃等號(hào),而多線程的額外開銷也沒有達(dá)到用戶能察覺的地步(即使很多權(quán)威的書籍中強(qiáng)掉線程的切換回帶來(lái)十分大的性能耗費(fèi),而實(shí)際的經(jīng)驗(yàn)都表明這消耗是十分難以擦覺的,個(gè)人認(rèn)為書中的出發(fā)點(diǎn)可能不同,對(duì)于cpu本身在加上現(xiàn)在windows動(dòng)不動(dòng)就2,3千條的線程也是是一個(gè)不應(yīng)該被忽視的內(nèi)容)
?
然后還是先講下線程的開銷吧
?
?
這些都是線程應(yīng)該具備的一些東西(windows),也就是說(shuō)創(chuàng)建一個(gè)線程就需要?jiǎng)?chuàng)建這樣?xùn)|西,剛剛我說(shuō)線程切換很難被察覺,可不是線程創(chuàng)建,創(chuàng)建大量的線程的確是需要大量的時(shí)間(所以高級(jí)的程序框架會(huì)提供線程池或類似的東西,緩解線程頻繁創(chuàng)建銷毀帶來(lái)的負(fù)面影響)
?
再談下線程的切換
?
然后時(shí)候操作系(windows)都只將一個(gè)線程分配給一個(gè)cpu(其實(shí)就是任何時(shí)候一個(gè)cpu都只能處理一個(gè)線程),線程允許運(yùn)行一個(gè)”時(shí)間片“一旦時(shí)間片到期windows就會(huì)進(jìn)行切換(對(duì)此還必須提一下windows可能會(huì)在時(shí)間片運(yùn)行中的任何時(shí)候進(jìn)行切換,同時(shí)時(shí)間片到期后也是可能選擇同一個(gè)線程進(jìn)行特殊的切換的)
讓我們看看切換的時(shí)候都要完成什么
?
?
一般情況下”時(shí)間片“大約為30毫秒,雖然書上是這樣寫的,不過(guò)實(shí)際測(cè)試結(jié)果遠(yuǎn)小于這個(gè)時(shí)間,可能是書寫的比較早吧,一般情況下這個(gè)時(shí)間片會(huì)根據(jù)操作系統(tǒng)的運(yùn)行負(fù)擔(dān)進(jìn)行自動(dòng)調(diào)節(jié)。
當(dāng)然時(shí)間片里的時(shí)間畢竟都是應(yīng)用程序本身的消耗,所以這些消耗都是有意義的,而對(duì)于線程切換則完全由系統(tǒng)跟cpu內(nèi)部完成,對(duì)于應(yīng)用程序業(yè)務(wù)本身來(lái)說(shuō)的確是浪費(fèi)。
好在如上面提到的實(shí)際上的消耗很難被察覺,一般情況下甚至很難被準(zhǔn)確的測(cè)試出確切的時(shí)間數(shù)據(jù),因?yàn)橄牡臅r(shí)間不能以我們常見的毫秒甚至是微秒來(lái)衡量,實(shí)際的測(cè)試只能說(shuō)明一次切換的消耗在個(gè)人計(jì)算機(jī)上一定是納米級(jí)別的
?
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.?
這里提供一個(gè)很權(quán)威的描述,就是上面的
講的是上下文切換也就是線程間的切換,是Linux的,不過(guò)線程的處理都是十分相似的。
which?can?be?on?the?order?of?nanoseconds?for?each?of?the?tens?or?hundreds?of?switches?per?second 。 我英文不是很好,大致是說(shuō)每秒幾百或幾十次的切換,而實(shí)際上只消耗了幾納秒。當(dāng)然現(xiàn)如今的windows的切換次數(shù)會(huì)大的多 借助工具我們可以看到線程切換的次數(shù)十分驚人,單qq這個(gè)進(jìn)程的所有活著的線程一共切換了3億多次,這個(gè)數(shù)量級(jí)帶來(lái)的性能消耗可能就不應(yīng)該被直接忽視掉。 不過(guò)事實(shí)上實(shí)際經(jīng)驗(yàn)發(fā)現(xiàn),由多線程帶來(lái)的性能消耗或程序業(yè)務(wù)處理能力的下降都不是由這正常的切換造成的,事實(shí)上線程的濫用是導(dǎo)致性能下降直接原因,而這些濫用是普遍存在的,在不該使用多線程的時(shí)候使用了它,頻繁的創(chuàng)建及銷毀線程,不正確的使用線程鎖,讓線程頻繁訪問(wèn)共享資源等等不合理操作導(dǎo)致多線程對(duì)資源或性能的消耗比系統(tǒng)切換來(lái)說(shuō),頁(yè)面的切換也就不止一提了轉(zhuǎn)載于:https://www.cnblogs.com/lulianqi/p/4808398.html
總結(jié)
- 上一篇: HDU 5439 Aggregated
- 下一篇: Python学习笔记 -- 第一章