317x Filetype PDF File size 0.41 MB Source: www.csc.villanova.edu
POSIX Threads (Pthreads) for
Shared Address Space
Programming
Pthreads
• Posix threads package
• Available on almost all machines (portable standard)
• Sort of like doing “parallel” (not “parallel for”) in OpenMP explicitly
• Basic calls:
• pthread_create: creates a thread, to execute a given function
• Pthread_join
• barrier, lock, mutex
• Thread private variables
• Many online resources:
• E.g., https://computing.llnl.gov/tutorials/pthreads/
L.V.Kale 2
Pthreads–Create and Join
• Spawn an attached thread • Detached threads
pthread_create • Join is not needed
(&thread1, NULL, foo, &arg) • The OS destroys thread
. resources when they
. terminate
. • A parameter in the create
pthread_join(thread1, status) call indicates a detached
thread
• Thread execution
void foo(&arg)
{ // Thread code
return(*status);
}
L.V.Kale 3
Executing a Thread
Main Program
. T
. Thread1 hr
e
. a
. void * func (void *arg) d1
s
pthread_create(&thread1, NULL, func, &arg); { t
a
. . c
. . k
. .
. .
return (status); M
pthread_join(thread1, status); } a
i
. n
s
t
. a
c
. k
.
L.V.Kale 4
no reviews yet
Please Login to review.