Upload
giorgio-natili
View
28
Download
0
Tags:
Embed Size (px)
Citation preview
WhatisMultithreading?Multi-ThreadingistheabilityofaCPUtoexecuteseveralthreadsofexecutionapparentlyatthesametime
Multi-threadingisawidespreadprogrammingandexecutionmodelthatallowsmultiplethreadstoexistwithinthecontextofasingleprocess
Multi~threading/tasking• Multitaskingisamethodbywhichmultipleprocessessharecommonprocessingresources• Multithreadingextendstheideaofmultitaskingintoapplications
Processes
8770mdworker0.000:00.114054756768K5760K0B0B104M2467M78770195sleeping
• Aprocessisaninstanceofaprograminexecution• Processesmaybeinoneoffivestates:New,Ready,Running,Waiting,Terminated
ThreadsAthreadisthesmallestsequenceofprogrammedinstructionsthatcanbemanagedindependentlybyanOSscheduler
KindofThreads• Userlevelthreads(youcanplaysafelywiththem)• Kernellevelthreads(lesssafelybutstillfunny!)• UserlevelthreadsareinthescopeofActionScriptandFlex
ThreadsandProcesses• Athreadiscontainedinsideaprocess• Multiplethreadscanexistwithinthesameprocessandshareresourcessuchasmemory
Differences(1/2)• Athreadislightweighttakinglesserresourcesthanaprocess• Processswitchingneedsinteractionwithoperatingsystemwhilethreadswitchingdoesnot• Eachprocessexecutesthesamecodeusingitsownresources,threadsusesameresources
Differences(2/2)• Whileonethreadisblockedandwaiting,secondthreadinthesametaskcanrun• Multipleprocessesusemoreresources,multiplethreadedprocessesusefewerresources• Processesoperateindependentlyoftheothers,threadsaccessotherthread'sdata
MultithreadingvsAsync• AsyncisthebetterindicatedfordoingheavyIOboundprocessing• Multithreadingisbetterindicatedwithlongrunningprocesses
TrivialParallelismScenario:largenumberofindependentcomputation-intensivetasks
• Createapoolofthreadsanddistributethetasksintothem(accordinglytoavailableCPUs)• Createamasterthreaddistributetheworkandcollecttheresults
Performances• Anextrathreadmeansmoresetup/teardowncosts• Implementmultithreadingonlyafterhavingprofiledyourapp• Lookfortaskswithasfewaspossibledependencies
ParallelAlgorithmsAlgorithmsthatconcurrentlyperformworkoncollectionsofdata
Fibonacci(n):1ifn<2:|threadA2returnn|threadA3x=spawnFibonacci(n-1)|threadA4y=spawnFibonacci(n-2)|threadB
WhyUsingMultithreading• Toimproveperformances(quickanddirtyanswer!)• ToavoidthattheGUIsuffersofthelatencyofeventdrivenarchitecture
Responsiveness• Keepheavytasksoutsidethemainthreadisthetraditionalwaytokeeptheappresponsive• Weactuallyloseoverallperformanceduetotheextracostofcontextswitching• Buttheuserexperienceisanywayimproved
Essentials• Multithreadingrequirecarefullyprogramming• Managethequeueofrequests• Coordinatingtheworkofmultiplethreads• Don'taccesstheUIfromanotUIthread• Trytoavoidcallingouttoexternalcodewhileholdingalock
MutualExclusion• Sometimeswewantthingstohappensimultaneously,andsometimesnot• Reasoningaboutconcurrentcomputationismostlyreasoningabouttime• Acquireandreleasesharedmembersinordertograntaccesstoonethreadpertime
ImplementingMutex• EachcriticalsectionisassociatedwithauniqueLockobject• Theaccesstothesectionislockedandunlockedwhentryingtoenterthecriticalsection
ConcurrentObjects• Whenmorethanonethreadworktogetherwearedealingwiththreadspools• Whenworkingwiththreadspoolseachthreadshouldbepausedandresumed• Managingconcurrencyit'spossibletoavoidblockingthreadsslowdowntheexectuintime
HandleRaceConditions• Iswhentwothreadsaccessthesamelocationinmemoryatthesametime(writing!)• Araceconditionisakindofbug,thathappensonlywithcertain
AvoidDeadlocksAdeadlockoccurswhenthewaitingprocessisstillholdingontoanotherresourcethatthefirstneedsbeforeitcanfinish
• Reducesthenumberofthreads(keepthingssimple!)• Don'tholdseverallocksatonce• Don'texecuteforeigncodewhileholdingalock
BeforeFlashPlayer11.3WhiletheunderlyingFlashPlayerandOperatingSystemusethreads,theexecutionmodelforActionscriptdoesnot
UIBlocking
if(appUI.isSlow){app.quality==='shittyapp'}
• Userinterfacenotresponsive• Theexpectedframeratedropssignificantly• Theuserexperiencewasdramaticallyimpacted
PseudoThreading• InvolvesENTER_FRAME,getTimer()andsomecalculation• NotalwaysreliableduetothegetTimer()reliability• Notsimpletomaintain
TheWorkerClass• AWorkerinstanceisavirtualinstanceoftheFlash/AIRruntime• EachWorkercontrolsandprovidesaccesstothelifecycleanddataofasingleworker• AworkerisjustanotherSWFthat’srunningalongsideyourmainSWF
WorkerSupport
if(Worker.isSupported){
}
• Desktop(FlashPlayer+AIR)• Mobile(AIRonAndroidonly,weird?!?)
CreateaWorkerWorkerDomain.current.createWorker(workerBytes);
TheworkerBytesvariablecontainstheByteArrayofanSWF,includeit:
• Usingthe[Embed]metatagtoembedthe.swffileintheapp
Isolation• Workersdonothaveaccesstothesamememory,variables,andcode• WorkersruninseparatedFlashPlayerinstances• Whatissharedbetweenworkersisunderyourcontrol
SharedProperties
setSharedProperty(key:String,value:*):void
• Eachworkercanexposeasetofpropertiestootherworkers• Eachworkercangetthevaluestoredinasharedproperty(fromanotherworker)• ValuesareNOTpassedbyreference(AMFserialization)
MessageChannel
while(mainToBack.messageAvailable){mainToBack.receive();}
• Sendone-waymessagesanddatafromoneworkertoanother• Treatsmessagesandrelatedvaluesasaqueue
SharedMemory• AShareableByteArray• Topassaroundareferencetoyoursharedmemory,youusetheMessageChannelAPI• WhenyoupassaByteArray,itisnotserialized,justreferenced• Youcanthenpassanythingyouwantbetweentheworkers(asrawbinarydata)
Mutex
varmutex:Mutex=newMutex();
• Locksaparticularblockofmemoryorsharedresource• AMutexmanagesaccessusingtheconceptofresourceownership
Condition
varcondition:Condition=Condition(mutex);
condition.wait(timeout:Number=-1);
• Suspendtheexecutionofaworker(i.e.thread)• Resumetheworkerexecutionunlockingtheassociatedmutex
Terminology• Promise,isanobjectwithathenmethod;itcanhave3states:pending,fulfilled,orrejected• Thenableisanobjectthatdefinesathenmethod• Reasonisavaluethatindicateswhyapromisewasrejected.
ApromiseApromiserepresentstheeventualresultofanasynchronousoperation
Thepointofpromisesistogiveusbackfunctionalcompositionanderrorbubblingintheasyncworld
DeferredAdeferredobjectcandothesameasapromiseobject,butithastwofunctionstotriggerthedone()andfail()functions
PromisesvsDeferred• Thepromiseobjectdoesnothaveresolve()orreject()functions• Apromiseissomethingsharedw/otherobjects,whileadeferredshouldbekeptprivate
Resolvers• AreusedinternallybyDeferredstocreate,resolveandrejectPromises• Resolverscanpropagatefulfillmentandrejection