计算机线程 外文翻译 外文文献 英文文献.docx

上传人:b****5 文档编号:6404209 上传时间:2023-01-06 格式:DOCX 页数:10 大小:27.40KB
下载 相关 举报
计算机线程 外文翻译 外文文献 英文文献.docx_第1页
第1页 / 共10页
计算机线程 外文翻译 外文文献 英文文献.docx_第2页
第2页 / 共10页
计算机线程 外文翻译 外文文献 英文文献.docx_第3页
第3页 / 共10页
计算机线程 外文翻译 外文文献 英文文献.docx_第4页
第4页 / 共10页
计算机线程 外文翻译 外文文献 英文文献.docx_第5页
第5页 / 共10页
点击查看更多>>
下载资源
资源描述

计算机线程 外文翻译 外文文献 英文文献.docx

《计算机线程 外文翻译 外文文献 英文文献.docx》由会员分享,可在线阅读,更多相关《计算机线程 外文翻译 外文文献 英文文献.docx(10页珍藏版)》请在冰豆网上搜索。

计算机线程 外文翻译 外文文献 英文文献.docx

计算机线程外文翻译外文文献英文文献

译文

Chapter25ThreadBasics

WhyDoesWindowsSupportThreads?

...………………………………………….…..…691

ThreadOverhead…..…………………………………………………………………….…...692

StoptheMadness…………..………………………………………………………….……...696

CPUTrends……………………………………………………………………….…..….……699

NUMAArchitectureMachines…..……………………………….………………….……..700

CLRThreadsandWindowsThreads………………………………..……………….…….703

UsingaDedicatedThreadtoPerformanAsynchronous

Compute-BoundOperation…………………………………………………………….……704

ReasonstoUseThreads……..……………………………………………...…………….….706

ThreadSchedulingandPriorities……………….……………………..……………….…...708

ForegroundThreadsversusBackgroundThreads………………………………………..713

WhatNow?

................................................................................................................................715

Inthischapter,Iintroducethebasicconceptsconcerningthreads,andIofferawayfordeveloperstoconceptualizeaboutthemandtheiruse.I’llexplainwhyMicrosoftWindowsintroducedtheconceptofthreads,CPUtrends,therelationshipbetweencommonlanguageruntime(CLR)threadsandWindowsthreads,theoverheadassociatedwithusingthreads,howWindowsschedulesthreads,theMicrosoft.NETFrameworkclassesthatexposethreadproperties,andmuchmore.

ThechaptersinPartVofthisbook,“Threading,”explainhowWindowsandtheCLRworktogethertoprovideathreadingarchitecture.Itismyhopethatafterreadingthesechapters,youwilltakeawayafoundationofknowledgethatwillallowyoutoeffectivelyusethreadstodesignandbuildresponsive,reliable,andscalableapplicationsandcomponents.

WhyDoesWindowsSupportThreads?

Backintheearlydaysofcomputers,operatingsystemsdidn’toffertheconceptofathread.Ineffect,therewasjustonethreadofexecutionthatranthroughouttheentiresystem,whichincludedbothoperatingsystemcodeandapplicationcode.Theproblemwithhavingonlyonethreadofexecutionwasthatalong-runningtaskwouldpreventothertasksfromexecuting.

Forexample,inthedaysof16-bitWindows,itwasverycommonforanapplicationthatwasprintingadocumenttostalltheentiremachine,causingtheOSandallotherapplicationstostopresponding.

And,sometimesapplicationswouldhaveabuginthem,resultinginaninfiniteloopthatalsostoppedtheentiremachinefromoperating.Atthispoint,theenduserwouldhavenochoicebuttorebootthecomputerbypressingtheresetbuttonorpowerswitch.Ofcourse,endusershateddoingthis(theystilldo,infact)becauseallrunningapplicationsterminated;moreimportantly,anydatathattheseapplicationswereprocessingwasthrownoutofmemoryandlost.Microsoftknewthat16-bitWindowswouldnotbeagoodenoughoperatingsystemtokeepMicrosoftrelevant

asthecomputerindustryprogressed,sotheysetouttobuildanewOStoaddresstheneedsofcorporationsandindividuals.ThisnewOShadtoberobust,reliable,scalable,andsecure,andithadtoimprovethemanydeficienciesof16-bitWindows.ThisOSkerneloriginallyshippedinMicrosoftWindowsNT.Overtheyears,thiskernelhashadmanytweaksandfeaturesaddedtoit.ThelatestversionofthiskernelshipsinthelatestversionsoftheMicrosoftclientandserverWindowsoperatingsystems.

WhenMicrosoftwasdesigningthisOSkernel,theydecidedtoruneachinstanceofanapplicationinwhatiscalledaprocess.Aprocessisjustacollectionofresourcesthatisusedbyasingleinstanceofanapplication.Eachprocessisgivenavirtualaddressspace,ensuringthatthecodeanddatausedbyoneprocessisnotaccessibletoanotherprocess.Thismakesapplicationinstancesrobustbecauseoneprocesscannotcorruptcodeordatabeingusedbyanother.Inaddition,theOS’skernelcodeanddataarenotaccessibletoprocesses;therefore,it’snotpossibleforapplicationcodetocorruptoperatingsystemcodeordata.Sonow,applicationcodecannotcorruptotherapplicationsortheOSitself,andthewholecomputingexperienceismuchbetterforendusers.Inaddition,thesystemismoresecurebecauseapplicationcodecannotaccessusernames,passwords,creditcardinformation,orothersensitiveinformationthatisinusebyanotherapplicationortheoperatingsystemitself.

Thisisallwellandgood,butwhatabouttheCPUitself?

Whatifanapplicationentersaninfiniteloop?

Well,ifthereisonlyoneCPUinthemachine,thenitexecutestheinfiniteloopandcannotexecuteanythingelse,sowhilethedatacannotbecorruptedandismoresecure,thesystemcouldstillstoprespondingtotheenduser.Microsoftneededtofixthisproblem,too,andthreadsweretheanswer.AthreadisaWindowsconceptwhosejobistovirtualizetheCPU.Windowsgiveseachprocessitsveryownthread(whichfunctionssimilartoaCPU),andifapplicationcodeentersaninfiniteloop,theprocessassociatedwiththatcodefreezesup,butotherprocesses(whichhavetheirownthreads)arenotfrozen;theykeeprunning!

ThreadOverhead

ThreadsareawesomebecausetheyenableWindowstoberesponsiveevenwhenapplicationsareexecutinglong-runningtasks.Also,threadsallowtheusertouseoneapplication(likeTaskManager)toforciblykillanapplicationthatappearsfrozenbecauseitisexecutingalong-runningtask.Butaswitheveryvirtualizationmechanism,threadshavespace(memoryconsumption)andtime(runtimeexecutionperformance)overheadassociatedwiththem.Let’sexplorethisoverheadinmoredetailnow.Everythreadhasoneofeachofthefollowing:

●ThreadkernelobjectTheOSallocatesandinitializesoneofthesedatastructuresforeachthreadcreatedinthesystem.Thedatastructurecontainsabunchofproperties(discussedlaterinthischapter)thatdescribethethread.Thisdatastructurealsocontainswhatiscalledthethread’scontext.ThecontextisablockofmemorythatcontainsasetoftheCPU’sregisters.WhenWindowsisrunningonamachinewithanx86CPU,thethread’scontextusesabout700bytesofmemory.Forx64andIA64CPUs,thecontextisabout1,240and2,500bytesofmemory,respectively.

●Threadenvironmentblock(TEB)TheTEBisablockofmemoryallocatedandinitializedinusermode(addressspacethatapplicationcodecanquicklyaccess).TheTEBconsumes1pageofmemory(4KBonx86andx64CPUs,8KBonanIA64CPU).TheTEBcontainstheheadofthethread’sexception-handlingchain.Eachtryblockthatthethreadentersinsertsanodeintheheadofthischain;thenodeisremovedfromthechainwhenthethreadexistsinthetryblock.Inaddition,theTEBcontainsthethread’sthread-localstoragedataaswellassomedatastructuresforusebyGraphicsDeviceInterface(GDI)andOpenGLgraphics.

●User-modestackTheuser-modestackisusedforlocalvariablesandargumentspassedtomethods.Italsocontainstheaddressindicatingwhatthethreadshouldexecutenextwhenthecurrentmethodreturns.Bydefault,Windowsallocates1MBofmemoryforeachthread’suser-modestack.

●Kernel-modestackThekernel-modestackisalsousedwhenapplicationcodepassesargumentstoakernel-modefunctionintheoperatingsystem.Forsecurityreasons,Windowscopiesanyargumentspassedfromuser-modecodetothekernelfromthethread’suser-modestacktothethread’skernel-modestack.Oncecopied,thekernelcanverifythearguments’values,andsincetheapplicationcodecan’taccessthekernelmodestack,theapplicationcan’tmodifythearguments’valuesaftertheyhavebeenvalidatedandtheOSkernelcodebeginstooperateonthem.Inaddition,thekernelcallsmethodswithinitselfandusesthekernel-modestacktopassitsownarguments,tostoreafunction’slocalvariables,andtostorereturnaddresses.Thekernel-modestackis12KBwhenrunningona32-bitWindowssystemand24KBwhenrunningona64-bitWindowssystem.

●DLLthread-attachandthread-detachnotificationsWindowshasapolicythatwheneverathreadiscreatedinaprocess,allDLLsloadedinthatprocesshavetheirDllMainmethodcalled,passingaDLL_THREAD_ATTACHflag.Similarly,wheneverathreaddies,allDLLsintheprocesshavetheirDllMainmethodcalled,passingitaDLL_THREAD_DETACHflag.SomeDLLsneedthesenotificationstoperformsomespecialinitializationorcleanupforeachthreadcreated/destroyedintheprocess.Forexample,theC-RuntimelibraryDLLallocatessomethread-localstoragestatethatisrequiredshouldthethreadusefunctionscontainedwithintheC-Runtimelibrary.

IntheearlydaysofWindows,manyprocesseshadmaybe5or6DLLsloadedintothem,buttoday,someprocesseshaveseveralhundredDLLsloadedintothem.Rightnow,onmymachine,MicrosoftOfficeOutlookhasabout250DLLsloadedintoitsprocessaddressspace!

ThismeansthatwheneveranewthreadiscreatedinOfficeOutlook,250DLLfunctionsmustgetcalledbeforethethreadisallowedtodowhatitwascreatedtodo.Andthese250functionsmustbecalledagainwheneverathreadinOutlookdies.Wow—thiscanseriouslyaffecttheperformanceofcreatinganddestroyingthreadswithinaprocess.

Sonow,youseeallthespaceandtimeoverheadthatisassociatedwithcreatingathread,lettingitsitaroundinthesystem,anddestroyingit.Butthesituationgetsevenworse—nowwe’regoingtostarttalkingaboutcontextswitching.AcomputerwithonlyoneCPUinitcandoonlyonethingatatime.Therefore,WindowshastosharetheactualCPUhardwareamongallthethreads(logicalCPUs)thataresittingaroundinthesystem.

Atanygivenmomentintime,WindowsassignsonethreadtoaCPU.Thatthreadisallowedtorunforatime-slice(sometimesreferredtoasaquantum).Whenthetime-sliceexpires,Windowscontextswitchestoanotherthread.EverycontextswitchrequiresthatWindowsperformsthef

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工程科技 > 能源化工

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1