38
Lecture 09: Programming with PThreads Concurrent and Mul:core Programming Department of Computer Science and Engineering Yonghong Yan [email protected] www.secs.oakland.edu/~yan 1

Lecture 09: Programming with PThreads · The POSIX Thread API • Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported by most vendors. – Implemented

  • Upload
    others

  • View
    12

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lecture 09: Programming with PThreads · The POSIX Thread API • Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported by most vendors. – Implemented

Lecture09:ProgrammingwithPThreads

ConcurrentandMul:coreProgramming

DepartmentofComputerScienceandEngineeringYonghongYan

[email protected]/~yan

1

Page 2: Lecture 09: Programming with PThreads · The POSIX Thread API • Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported by most vendors. – Implemented

Topics(Part1)

•  IntroducAon•  Principlesofparallelalgorithmdesign(Chapter3)•  Programmingonsharedmemorysystem(Chapter7)

–  OpenMP–  PThread,mutualexclusion,locks,synchroniza:ons–  Cilk/Cilkplus

•  AnalysisofparallelprogramexecuAons(Chapter5)–  PerformanceMetricsforParallelSystems

•  Execu:onTime,Overhead,Speedup,Efficiency,Cost–  ScalabilityofParallelSystems–  Useofperformancetools

2

Page 3: Lecture 09: Programming with PThreads · The POSIX Thread API • Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported by most vendors. – Implemented

ShortReview

•  Parallelalgorithmdesign1.  TasksandDecomposiAon

•  TheoryandpracAce(axpy,matvecandmatmul)2.   ProcessesandMapping3.  MinimizingInteracAonOverheads

•  PracAceontaskanddecomposi:on–  AXPY,MatrixvectormulAplicaAon,matrixmatrixmulAplicaAon

3

Page 4: Lecture 09: Programming with PThreads · The POSIX Thread API • Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported by most vendors. – Implemented

OpenMP:WorksharingConstructs

4

for(i=0;i<N;i++) { a[i] = a[i] + b[i]; }

#pragma omp parallel shared (a, b)

{

int id, i, Nthrds, istart, iend; id = omp_get_thread_num(); Nthrds = omp_get_num_threads(); istart = id * N / Nthrds; iend = (id+1) * N / Nthrds; for(i=istart;i<iend;i++) { a[i] = a[i] + b[i]; }

}

#pragma omp parallel shared (a, b) private (i) #pragma omp for schedule(static)

for(i=0;i<N;i++) { a[i] = a[i] + b[i]; }

Sequential code

OpenMP parallel region

OpenMP parallel region and a worksharing for construct

Page 5: Lecture 09: Programming with PThreads · The POSIX Thread API • Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported by most vendors. – Implemented

•  DirecAvesimplementedviacodemodificaAonandinserAonofrunAmelibrarycalls

–  Basicstepisoutliningofcodeinparallelregion

•  RunAmelibraryresponsibleformanagingthreads

–  Schedulingloops–  Schedulingtasks–  ImplemenAng

synchronizaAon•  ImplementaAoneffortis

reasonable

OpenMP Code Translation

int main(void) { int a,b,c; #pragma omp parallel \ private(c) do_sth(a,b,c); return 0; }

_INT32 main() { int a,b,c; /* microtask */ void __ompregion_main1() { _INT32 __mplocal_c; /*shared variables are kept intact, substitute accesses to private variable*/ do_sth(a, b, __mplocal_c); } … /*OpenMP runtime calls */ __ompc_fork(&__ompregion_main1); … }

Eachcompilerhascustomrun-Amesupport.QualityoftherunAmesystemhasmajorimpactonperformance.

StandardOpenMPImplementa:on

Page 6: Lecture 09: Programming with PThreads · The POSIX Thread API • Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported by most vendors. – Implemented

OpenMPAPI

ApplicaAonwriter

OpenMPRTLAPI

PthreadAPI

Compilerwriter

Librarywriter

main(){#pragmaompparallelprin^("Hello,world.!\n");

main(){…__ompc_fork(0,&__ompregion_main1,

reg__7);…}void__ompregion_main1(__ompv_gAd_a__0,

__ompv_slink_a__0){…prin^((const_INT8*)(_INT8(*)[15LL])"Hello,world.!\n");}

….for(i=1;i<threads_to_create;i++){return_value=pthread_create(

&(__omp_level_1_pthread[i].uthread_id),&__omp_pthread_aqr,(pthread_entry)__ompc_level_1_slave,(void*)((unsignedlongint)i));

OpenMPImplementa:on

Page 7: Lecture 09: Programming with PThreads · The POSIX Thread API • Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported by most vendors. – Implemented

Execu:onModelStart

End

N-1threads

WaitonCondiAonvar.

ExecuteMicro_task

Parallelregion1

----------------------Execute

Micro_task

Parallelregionn----------------------

ExecuteMicro_task

Cleanup

signal

signal

M-1Nestedthreads

ExecutenestedMicro_task

MasterthreadLevel1slavethread

ReusedNestedslavethread

IniAalizaAon

Pthread_exit()

Page 8: Lecture 09: Programming with PThreads · The POSIX Thread API • Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported by most vendors. – Implemented

PThread

•  ProcessingElementabstrac:onforsoSware–  PThreads–  OpenMP/Cilk/othersrun:meusePThreadsfortheir

implementa:on

•  Thefounda:onofparallelismfromcomputersystem

•  TopicOverview–  ThreadbasicsandthePOSIXThreadAPI–  Threadcrea:on,termina:onandjoining–  Threadsafety–  Synchroniza:onprimi:vesinPThreads

Page 9: Lecture 09: Programming with PThreads · The POSIX Thread API • Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported by most vendors. – Implemented

WhatisaThread

•  OSview–  AnindependentstreamofinstrucAonsthatcanbescheduledtorunbytheOS.

•  Soswaredeveloperview–  A“procedure”thatrunsindependentlyfromthemainprogram

•  ImaginemulAplesuchproceduresofmainrunsimultaneouslyand/orindependently

–  SequenAalprogram:asinglestreamofinstrucAonsinaprogram.

–  MulA-threadedprogram:aprogramwithmulAplestreams•  MulAplethreadsareneededtousemulAplecores/CPUs

•  Athreadisavirtualrepresenta:onofahardwarecore9

Page 10: Lecture 09: Programming with PThreads · The POSIX Thread API • Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported by most vendors. – Implemented

Threadas“func:oninstance”

X=

Athreadisasinglestreamofcontrolintheflowofaprogram:

!for (i = 0; i < n; i++) y[i] = dot_product(row(A, i),b);

for (i = 0; i < n; i++) y[i] = create_thread(dot_product(row(A, i), b));

•  thinkofthethreadasaninstanceofafuncAonthatreturnsbeforethefuncAonhasfinishedexecuAng.

Page 11: Lecture 09: Programming with PThreads · The POSIX Thread API • Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported by most vendors. – Implemented

•  processescontaininformaAonaboutprogramresourcesandprogramexecuAonstate,including:–  ProcessID,processgroupID,userID,andgroupID–  Environment,Workingdirectory,PrograminstrucAons–  Registers,Stack,Heap–  Filedescriptors,SignalacAons–  Sharedlibraries,Inter-processcommunicaAontools(suchas

messagequeues,pipes,semaphores,orsharedmemory).

•  Whenwerunaprogram,aprocessiscreated–  E.g../a.out,./axpy,etc–  fork()systemcall

Processes

Page 12: Lecture 09: Programming with PThreads · The POSIX Thread API • Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported by most vendors. – Implemented

•  Threadsuse,andexistwithin,theprocessresources•  ScheduledandrunasindependentenAAes•  DuplicateonlythebareessenAalresourcesthatenablethemtoexistasexecutablecode

Threads

Page 13: Lecture 09: Programming with PThreads · The POSIX Thread API • Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported by most vendors. – Implemented

•  Athreadmaintainsitsown:–  Stackpointer–  Registers–  SchedulingproperAes(suchaspolicy

orpriority)–  Setofpendingandblockedsignals–  Threadspecificdata.

•  MulAplethreadssharetheprocessresources

•  Athreaddiesiftheprocessdies•  "lightweight”forcreaAngand

terminaAngthreadsthatforprocesses

Threads

Page 14: Lecture 09: Programming with PThreads · The POSIX Thread API • Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported by most vendors. – Implemented

POSIXthreads(Pthreads)

•  ThreadsusedtoimplementparallelisminsharedmemorymulAprocessorsystems,suchasSMPs

•  Historically,hardwarevendorshaveimplementedtheirownproprietaryversionsofthreads–  Portabilityaconcernforsoswaredevelopers.

•  ForUNIXsystems,astandardizedClanguagethreadsprogramminginterfacehasbeenspecifiedbytheIEEEPOSIX1003.1cstandard.–  ImplementaAonsthatadheretothisstandardarereferredto

asPOSIXthreads

14

Page 15: Lecture 09: Programming with PThreads · The POSIX Thread API • Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported by most vendors. – Implemented

ThePOSIXThreadAPI

•  CommonlyreferredtoasPthreads,POSIXhasemergedasthestandardthreadsAPI,supportedbymostvendors.–  Implementedwithapthread.hheader/includefileandathread

library

•  FuncAonaliAes–  Threadmanagement,e.g.creaAonandjoining–  ThreadsynchronizaAonprimiAves

•  Mutex•  CondiAonvariables•  Reader/writerlocks•  Pthreadbarrier

–  Thread-specificdata

•  TheconceptsdiscussedherearelargelyindependentoftheAPI–  AppliedtootherthreadAPIs(NTthreads,Solaristhreads,Java

threads,etc.)aswell.

Page 16: Lecture 09: Programming with PThreads · The POSIX Thread API • Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported by most vendors. – Implemented

PThreadAPI

•  #include <pthread.h> !

•  gcc -lpthread

16

Page 17: Lecture 09: Programming with PThreads · The POSIX Thread API • Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported by most vendors. – Implemented

•  IniAally,main()programcomprisesasingle,defaultthread–  Allotherthreadsmustbeexplicitlycreated

int pthread_create( pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void * arg);

•  thread:Anopaque,uniqueidenAfierforthenewthreadreturnedbythesubrouAne

•  aZr:AnopaqueaqributeobjectthatmaybeusedtosetthreadaqributesYoucanspecifyathreadaqributesobject,orNULLforthedefaultvalues

•  start_rou:ne:theCrouAnethatthethreadwillexecuteonceitiscreated•  arg:Asingleargumentthatmaybepassedtostart_rou+ne.Itmustbepassedby

referenceasapointercastoftypevoid.NULLmaybeusedifnoargumentistobepassed.

ThreadCrea:on

Opaqueobject:Aleqerisanopaqueobjecttothemailman,andsenderandreceiverknowtheinformaAon.

Page 18: Lecture 09: Programming with PThreads · The POSIX Thread API • Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported by most vendors. – Implemented

ThreadCrea:on

•  pthread_createcreatesanewthreadandmakesitexecutable,i.e.runimmediatelyintheory–  canbecalledanynumberofAmesfromanywherewithinyourcode

•  Oncecreated,threadsarepeers,andmaycreateotherthreads•  Thereisnoimpliedhierarchyordependencybetweenthreads

18

Page 19: Lecture 09: Programming with PThreads · The POSIX Thread API • Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported by most vendors. – Implemented

#include <pthread.h> #define NUM_THREADS 5 void *PrintHello(void *thread_id) { long tid = (long)thread_id; printf("Hello World! It's me, thread #%ld!\n", tid); pthread_exit(NULL); } int main(int argc, char *argv[]) { pthread_t threads[NUM_THREADS]; long t; for(t=0;t<NUM_THREADS;t++) { printf("In main: creating thread %ld\n", t); int rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t ); if (rc) { printf("ERROR; return code from pthread_create() is %d\n", rc); exit(-1); } } pthread_exit(NULL); }

One possible output: In main: creating thread 0 In main: creating thread 1 In main: creating thread 2 In main: creating thread 3 Hello World! It's me, thread #0! In main: creating thread 4 Hello World! It's me, thread #1! Hello World! It's me, thread #3! Hello World! It's me, thread #2! Hello World! It's me, thread #4!

Example1:pthread_create

Page 20: Lecture 09: Programming with PThreads · The POSIX Thread API • Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported by most vendors. – Implemented

•  pthread_exitisusedtoexplicitlyexitathread

–  Calledaserathreadhascompleteditsworkandisnolongerrequiredtoexist

•  Ifmain()finishesbeforethethreadsithascreated–  Ifexitswithpthread_exit(),theotherthreadswillconAnueto

execute

–  Otherwise,theywillbeautomaAcallyterminatedwhenmain()finishes

•  TheprogrammermayopAonallyspecifyaterminaAonstatus,whichisstoredasavoidpointerforanythreadthatmayjointhecallingthread

•  Cleanup:thepthread_exit()rouAnedoesnotclosefiles–  Anyfilesopenedinsidethethreadwillremainopenaserthethread

isterminated

Termina:ngThreads

Page 21: Lecture 09: Programming with PThreads · The POSIX Thread API • Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported by most vendors. – Implemented

ThreadAZribute int pthread_create( pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void * arg);

•  Aqributecontainsdetailsabout–  whetherschedulingpolicyisinheritedorexplicit–  schedulingpolicy,schedulingpriority–  stacksize,stackguardregionsize

•  pthread_attr_initandpthread_attr_destroyareusedtoiniAalize/destroythethreadaqributeobject

•  OtherrouAnesarethenusedtoquery/setspecificaqributesinthethreadaqributeobject

21

Page 22: Lecture 09: Programming with PThreads · The POSIX Thread API • Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported by most vendors. – Implemented

•  Thepthread_create()rouAnepermitstheprogrammertopassoneargumenttothethreadstartrouAne

•  ForcaseswheremulApleargumentsmustbepassed:–  Createastructurewhichcontainsallofthearguments–  Thenpassapointertotheobjectofthatstructureinthe

pthread_create()rouAne.–  Allargumentsmustbepassedbyreferenceandcastto(void*)

•  Makesurethatallpasseddataisthreadsafe:dataracing–  itcannotbechangedbyotherthreads

–  Itcanbechangedinadeterminantway•  ThreadcoordinaAon

PassingArgumentstoThreads

Page 23: Lecture 09: Programming with PThreads · The POSIX Thread API • Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported by most vendors. – Implemented

#include <pthread.h> #define NUM_THREADS 8 struct thread_data { int thread_id; char *message; }; struct thread_data thread_data_array[NUM_THREADS]; void *PrintHello(void *threadarg) { int taskid; char *hello_msg; sleep(1); struct thread_data *my_data = (struct thread_data *) threadarg; taskid = my_data->thread_id; hello_msg = my_data->message; printf("Thread %d: %s\n", taskid, hello_msg); pthread_exit(NULL); }

Example2:ArgumentPassing

Page 24: Lecture 09: Programming with PThreads · The POSIX Thread API • Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported by most vendors. – Implemented

int main(int argc, char *argv[]) { pthread_t threads[NUM_THREADS]; int t; char *messages[NUM_THREADS]; messages[0] = "English: Hello World!"; messages[1] = "French: Bonjour, le monde!"; messages[2] = "Spanish: Hola al mundo"; messages[3] = "Klingon: Nuq neH!"; messages[4] = "German: Guten Tag, Welt!"; messages[5] = "Russian: Zdravstvytye, mir!"; messages[6] = "Japan: Sekai e konnichiwa!"; messages[7] = "Latin: Orbis, te saluto!"; for(t=0;t<NUM_THREADS;t++) { struct thread_data * thread_arg = &thread_data_array[t]; thread_arg->thread_id = t; thread_arg->message = messages[t]; pthread_create(&threads[t], NULL, PrintHello, (void *) thread_arg); } pthread_exit(NULL); }

Example2:ArgumentPassing

Thread3:Klingon:NuqneH!Thread0:English:HelloWorld!Thread1:French:Bonjour,lemonde!Thread2:Spanish:HolaalmundoThread5:Russian:Zdravstvytye,mir!Thread4:German:GutenTag,Welt!Thread6:Japan:Sekaiekonnichiwa!Thread7:LaAn:Orbis,tesaluto!

Page 25: Lecture 09: Programming with PThreads · The POSIX Thread API • Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported by most vendors. – Implemented

WaitforThreadTermina:on

Suspendexecu:onofcallingthreadun:lthreadterminates#include <pthread.h> int pthread_join(

pthread_t thread, void **value_ptr);•  thread:thejoiningthread•  value_ptr:ptrtolocaAonforreturncodeaterminaAngthreadpassesto

pthread_exit

•  ItisalogicalerrortoaqemptsimultaneousmulAplejoinsonthesamethread25

Page 26: Lecture 09: Programming with PThreads · The POSIX Thread API • Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported by most vendors. – Implemented

#include <pthread.h> #define NUM_THREADS 4 void *BusyWork(void *t) { int i; long tid = (long)t; double result=0.0;

printf("Thread %ld starting...\n",tid);

for (i=0; i<1000000; i++) { result = result + sin(i) * tan(i); }

printf("Thread %ld done. Result = %e\n",tid, result); pthread_exit((void*) t); }

Example3:PthreadJoining

Page 27: Lecture 09: Programming with PThreads · The POSIX Thread API • Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported by most vendors. – Implemented

int main (int argc, char *argv[]) { pthread_t thread[NUM_THREADS]; pthread_attr_t attr; long t; void *status; /* Initialize and set thread detached attribute */ pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); for(t=0; t<NUM_THREADS; t++) { printf("Main: creating thread %ld\n", t); pthread_create(&thread[t], &attr, BusyWork, (void *)t); } /* Free attribute and wait for the other threads */ pthread_attr_destroy(&attr); for(t=0; t<NUM_THREADS; t++) { pthread_join(thread[t], &status); printf(“Main: joined with thread %ld, status: %ld\n", t, (long)status); } printf("Main: program completed. Exiting.\n"); pthread_exit(NULL); }

Example3:PthreadjoiningMain:creaAngthread0Main:creaAngthread1Thread0starAng...Main:creaAngthread2Thread1starAng...Main:creaAngthread3Thread2starAng...Thread3starAng...Thread1done.Result=-3.153838e+06Thread0done.Result=-3.153838e+06Main:joinedwiththread0,status:0Main:joinedwiththread1,status:1Thread2done.Result=-3.153838e+06Main:joinedwiththread2,status:2Thread3done.Result=-3.153838e+06Main:joinedwiththread3,status:3Main:programcompleted.ExiAng.

Page 28: Lecture 09: Programming with PThreads · The POSIX Thread API • Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported by most vendors. – Implemented

•  Allthreadshaveaccesstothesameglobal,sharedmemory•  Threadsalsohavetheirownprivatedata•  Programmersareresponsibleforsynchronizingaccess

(protecAng)globallyshareddata.

SharedMemoryandThreads

Page 29: Lecture 09: Programming with PThreads · The POSIX Thread API • Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported by most vendors. – Implemented

ThreadConsequences

29

•  SharedState!–  Accidentalchangestoglobalvariablescanbefatal.–  Changesmadebyonethreadtosharedsystemresources(suchas

closingafile)willbeseenbyallotherthreads–  Twopointershavingthesamevaluepointtothesamedata–  ReadingandwriAngtothesamememorylocaAonsispossible–  ThereforerequiresexplicitsynchronizaAonbytheprogrammer

•  ManylibraryfuncAonsarenotthread-safe–  LibraryFuncAonsthatreturnpointerstostaAcinternalmemory.E.g.

gethostbyname()•  Lackofrobustness

–  CrashinonethreadwillcrashtheenAreprocess

Page 30: Lecture 09: Programming with PThreads · The POSIX Thread API • Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported by most vendors. – Implemented

•  Thread-safeness:inanutshell,refersanapplicaAon'sabilitytoexecutemulAplethreadssimultaneouslywithout"clobbering"shareddataorcreaAng"race"condiAons

•  Example:anapplicaAoncreatesseveralthreads,eachofwhichmakesacalltothesamelibraryrouAne:–  ThislibraryrouAneaccesses/modifiesaglobalstructureor

locaAoninmemory.–  AseachthreadcallsthisrouAneitispossiblethattheymaytry

tomodifythisglobalstructure/memorylocaAonatthesameAme.

–  IftherouAnedoesnotemploysomesortofsynchronizaAonconstructstopreventdatacorrupAon,thenitisnotthread-safe.

Thread-safeness

Page 31: Lecture 09: Programming with PThreads · The POSIX Thread API • Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported by most vendors. – Implemented

Thread-safeness

Page 32: Lecture 09: Programming with PThreads · The POSIX Thread API • Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported by most vendors. – Implemented

Theimplica:ontousersofexternallibraryrou:nes:

•  Ifyouaren't100%certaintherouAneisthread-safe,thenyoutakeyourchanceswithproblemsthatcouldarise.

•  Recommenda:on–  BecarefulifyourapplicaAonuseslibrariesorotherobjectsthat

don'texplicitlyguaranteethread-safeness.–  Whenindoubt,assumethattheyarenotthread-safeunAl

provenotherwise–  Thiscanbedoneby"serializing"thecallstotheuncertain

rouAne,etc.

Thread-safeness

Page 33: Lecture 09: Programming with PThreads · The POSIX Thread API • Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported by most vendors. – Implemented

Example4:DataRacing

33

#include <pthread.h> #define NUM_THREADS 5 void *PrintHello(void *thread_id) { /* thread func */ long tid = *((long*)thread_id); printf("Hello World! It's me, thread #%ld!\n", tid); pthread_exit(NULL); } int main(int argc, char *argv[]) { pthread_t threads[NUM_THREADS]; long t; for(t=0;t<NUM_THREADS;t++) { printf("In main: creating thread %ld\n", t); int rc = pthread_create(&threads[t], NULL, PrintHello, (void *)&t ); if (rc) { printf("ERROR; return code from pthread_create() is %d\n", rc); exit(-1); } } pthread_exit(NULL); }

Inmain:creaAngthread0Inmain:creaAngthread1Inmain:creaAngthread2Inmain:creaAngthread3HelloWorld!It'sme,thread#3!HelloWorld!It'sme,thread#3!HelloWorld!It'sme,thread#3!Inmain:creaAngthread4HelloWorld!It'sme,thread#4!HelloWorld!It'sme,thread#5!

Page 34: Lecture 09: Programming with PThreads · The POSIX Thread API • Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported by most vendors. – Implemented

•  TheprimarymoAvaAon–  TorealizepotenAalprogramperformancegains

•  ComparedtothecostofcreaAngandmanagingaprocess–  AthreadcanbecreatedwithmuchlessOSoverhead

•  Managingthreadsrequiresfewersystemresourcesthanmanagingprocesses

•  Allthreadswithinaprocesssharethesameaddressspace

•  Inter-threadcommunicaAonismoreefficientand,inmanycases,easiertousethaninter-processcommunicaAon

WhyPthreads(notprocesses)?

Page 35: Lecture 09: Programming with PThreads · The POSIX Thread API • Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported by most vendors. – Implemented

•  Timingresultsforthefork()subrouAneandthepthreads_create()subrouAne–  Timingsreflect50,000process/threadcreaAons–  unitsareinseconds–  noopAmizaAonflags

pthread_createvsfork

Page 36: Lecture 09: Programming with PThreads · The POSIX Thread API • Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported by most vendors. – Implemented

•  PotenAalperformancegainsandpracAcaladvantagesovernon-threadedapplicaAons:

–  OverlappingCPUworkwithI/O•  Forexample,aprogrammayhavesecAonswhereitisperformingalongI/OoperaAon

•  WhileonethreadiswaiAngforanI/Osystemcalltocomplete,CPUintensiveworkcanbeperformedbyotherthreads.

•  Priority/real-Amescheduling–  Taskswhicharemoreimportantcanbescheduledtosupersedeor

interruptlowerprioritytasks.

•  Asynchronouseventhandling–  TaskswhichserviceeventsofindeterminatefrequencyandduraAoncanbe

interleaved–  Forexample,awebservercanbothtransferdatafrompreviousrequests

andmanagethearrivalofnewrequests.

Whypthreads

Page 37: Lecture 09: Programming with PThreads · The POSIX Thread API • Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported by most vendors. – Implemented

AXPYwithPThreads

•  y=α·x+y–  xandyarevectorsofsizeN

•  InC,x[N],y[N]–  αisscalar

•  DecomposiAonandmappingtopthreads

37

Ataskwillbemappedtoapthread

Page 38: Lecture 09: Programming with PThreads · The POSIX Thread API • Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported by most vendors. – Implemented

AXPYwithPThreads

38