Upload
others
View
4
Download
0
Embed Size (px)
Citation preview
ThreadManagement(join)
Joinblocksthecallingthreaduntilthetargetthreadterminates,andreturnsit’sthread_exit argument.Itisimpossible tojoinathreadinadetachedstate.Itisalsoimpossibletoreattachit
intpthread_join (pthread_t thread,void**value_ptr)[OUT]threadreturnvalue
int pthread_detach (pthread_t thread)int pthread_attr_setdetachstate (pthread_attr_t *attr, [IN/OUT]attribute
int detachstate)[IN]statetobesetint pthread_attr_getdetachstate (const pthread_attr_t *attr,
int *detachstate)[OUT]detachstatevalue
ThreadManagement(state)
ThePOSIXstandarddoesnotdictatethesizeofathread'sstack!
int pthread_attr_getstacksize (const pthread_attr_t *restrictattr,size_t *restrictstacksize)
int pthread_attr_setstacksize (pthread_attr_t *attr,size_t stacksize)int pthread_attr_getstackaddr (const pthread_attr_t *restrictattr,
void**restrictstackaddr)int pthread_attr_setstackaddr (pthread_attr_t *attr,void*stackaddr)pthread_t pthread_self (void)int pthread_equal (pthread_t t1,pthread_t t2)
Example:AProducer– ConsumerQueue
• Wehaveaboundedqueuewhereproducersstoretheiroutputandfromwhereconsumerstaketheirinput
• Protectthestructureagainstintensiveunnecessaryaccesses– Detectboundaryconditions:queueemptyandqueuefull
Boun
dedQue
ue
…producers
…consumers
Example:dot-product
• Dividethearraysbetweenparticipantstoload-balancethework– Eachwillthencomputeapartialsum
• Addallthepartialsumstogetherforthefinalresult(reduceoperation)
• Technicaldetails:costofmanagingthethreadsvs.costofthealgorithm?Howtominimizethemanagementcost?
Scalarproduct,innerproduct
=PN
n=1 ai.bi
a
b
a
b
Thr 1 Thr 2 Thr 3
Partialsum
Partialsum
Partialsum
+
+
Homework:thediningphilosophersproblem
Dijkstra,1965:Fivesilentphilosopherssitataroundtablewithbowlsofspaghetti.Forksareplacedbetweeneachpairofadjacentphilosophers.Eachphilosophermustalternatelythinkandeat.However,aphilosophercanonlyeatspaghettiwhenhehasbothleftandrightforks.Eachforkcanbeheldbyonlyonephilosopherandsoaphilosophercanusetheforkonlyifitisnotbeingusedbyanotherphilosopher.Afterhefinisheseating,heneedstoputdownbothforkssotheybecomeavailabletoothers.Aphilosophercantaketheforkonhisrightortheoneonhisleftastheybecomeavailable,butcannotstarteatingbeforegettingbothofthem.Eatingisnotlimitedbytheremainingamountsofspaghettiorstomachspace;aninfinitesupplyandaninfinitedemandareassumed.
2
15
4
3
Deliverin2weeks(Friday09/09):Apdf documentwiththedescriptionoftheproblemasyouunderstanditandwiththesolutionyouchoosetoimplement.Implementasolutionforanunboundednumberofphilosophers,whereeachphilosopherisimplementedasathread,andtheforksarethesynchronizationsneededbetweenthem.ProvidetheCsourcecodeandamakefile,allowingforsmoothcompilationandexecution.