Parallel Computing Unit 4 - Pthreads
Parallel Computing Unit 4 - Pthreads
Parallel Computing
Parallel Programming
• By default, a thread is created with certain attributes. Some of these attributes can be
changed by the programmer via the thread attribute object.
• pthread_attr_init and pthread_attr_destroy are used to initialize/destroy the thread
attribute object.
• Other routines are then used to query/set specific attributes in the thread attribute
object. Attributes include:
• Detached or joinable state
• Scheduling inheritance
• Scheduling policy
• Scheduling parameters
• Scheduling contention scope
• Stack size
• Stack address
• Stack guard (overflow) size
#include <iostream>
Have 4 arguments:
./test
a1
b2
c3
int rc;
long t;
• pthread_join (threadid,status)
• pthread_detach (threadid)
• pthread_attr_setdetachstate (attr,detachstate)
• pthread_attr_getdetachstate (attr,detachstate)
Joining:
• "Joining" is one way to accomplish synchronization between threads. For example
• The pthread_join() subroutine blocks the calling thread until the specified threadid thread terminates.
• A joining thread can match one pthread_join() call. It is a logical error to attempt multiple joins on the same
thread
pthread_mutex_init (mutex,attr)
pthread_mutex_destroy (mutex)
pthread_mutexattr_init (attr)
pthread_mutexattr_destroy (attr)
pthread_mutex_init()
• This method permits setting mutex object attributes, attr
• pthread_mutex_destroy() should be used to free a mutex object which is no
longer needed