5
Thread Management (join) Join blocks the calling thread until the target thread terminates, and returns it’s thread_exit argument. It is impossible to join a thread in a detached state. It is also impossible to reattach it int pthread_join (pthread_t thread, void **value_ptr) [OUT] thread return value int pthread_detach (pthread_t thread) int pthread_attr_setdetachstate (pthread_attr_t *attr, [IN/OUT] attribute int detachstate) [IN] state to be set int pthread_attr_getdetachstate (const pthread_attr_t *attr, int *detachstate) [OUT] detach state value

Thread Management (join) - ICL UTKluszczek/teaching/courses/fall2016...Thread Management (join) Join blocks the calling thread until the target thread terminates, and returns it’s

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Thread Management (join) - ICL UTKluszczek/teaching/courses/fall2016...Thread Management (join) Join blocks the calling thread until the target thread terminates, and returns it’s

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

Page 2: Thread Management (join) - ICL UTKluszczek/teaching/courses/fall2016...Thread Management (join) Join blocks the calling thread until the target thread terminates, and returns it’s

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)

Page 3: Thread Management (join) - ICL UTKluszczek/teaching/courses/fall2016...Thread Management (join) Join blocks the calling thread until the target thread terminates, and returns it’s

Example:AProducer– ConsumerQueue

• Wehaveaboundedqueuewhereproducersstoretheiroutputandfromwhereconsumerstaketheirinput

• Protectthestructureagainstintensiveunnecessaryaccesses– Detectboundaryconditions:queueemptyandqueuefull

Boun

dedQue

ue

…producers

…consumers

Page 4: Thread Management (join) - ICL UTKluszczek/teaching/courses/fall2016...Thread Management (join) Join blocks the calling thread until the target thread terminates, and returns it’s

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

+

+

Page 5: Thread Management (join) - ICL UTKluszczek/teaching/courses/fall2016...Thread Management (join) Join blocks the calling thread until the target thread terminates, and returns it’s

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.