COMP5426 Distributed
Programming Shared Address
Platforms (Pthreads)
Copyright By PowCoder代写 加微信 powcoder
The logical machine model of a thr parallel programming paradigm.
Memory in the logical machine model is globally accessible to every thread.
The stack corresponding to the function call is generally treated as being local to the thread for liveness reasons.
Cannot assume any or
Any such order m established using
ust be explicitly synchronization
Issues on deadlock, starvation
The Pthreads
The subroutin
Condition v
PI is defined
SIX 1003.1 –
Pthreads API can be informally grouped into three major classes:
The first class of f
– creating,
thread attributes (joinable, sc
Most of time it is suff
unctions work dire
detaching,
heduling et
is to be passed.
If successful,
Initially, only a single, default thread. All other threads must be explicitly created by the programmer.
routine: pthread_create(thread arg)
thread: unique identifier for the new thread (pthread_t)
pthread_create() f
return zero; otherwise, an error number shall be
returned to indicate the error.
, attr, st
thread attributes. read attributes obje
art_routine,
attr: attribute object used to set (pthread_attr) You can specify a th NULL for the default values.
start_routine: C routine that the thread will execute.
arg: A single argument that may be passed to start_routine. It must be passed by reference. NULL may be used if no argument
By default, a thread is created Some of these attributes can b
er via the thread attribute object.
pthread_attr_init(attr) and pthread_attr_destroy(attr) are used to initialize/destroy the thread attribute object.
Other routines are then used to query/set specific attributes in the thread attribute object.
with certain attributes. e changed by the
pthread_exit(s t a t u s )
used to explicitly exit a thread.
rogrammer may optionally specify a termination status, which is stored as a void pointer for any thread that may join the calling thread.
Cleanup: the pthread_exit()
any files opened inside the thread will remai
thread is terminated.
routine does not close files;
#include
Must include pthr
read #%d!\n”, *tid);
void *PrintHello(void *threadid) { int *tid;
tid = (int *) threadid;
printf(“Hello World! It’s me, th
pthread_exit(NULL);
ain(int ar
pthread_exit(NULL);
gcc –lpthre
t rc, t, tids[NUM_THREA
char *argv[]) {
pthread_t threads[NUM_THREADS];
for(t=0;t
a2 = argp->arg2;
…free (argp);
pthread_exit(NULL);
int main(int argc,
pthread_t t;
struct two_args *ap; int rc;
…ap = (struct two_args *) ap->arg1 = 1;
ap->arg2 = 2;
rc = pthread_create
d_exit(NULL);
char *argv
(&t, NULL,
malloc (sizeof
ct two_args));
needs_2_args, (void *) ap);
pthread_join (threadid,stat pthread_detach (threadid,s
pthread_attr_setdatachstat
pthread_attr_getdet
us) tatus)
e (attr,detachstate)
achstate (attr,da
“Joining” is one wa between threads.
The pthread_join() subroutine blocks the calling thread until the specified threadid thread terminates.
The programmer is able to obtain the target threa termination return status if it was specified in the target thread’s call to pthread_exit().
accomplish synch
tachstate)
ronization
When a thread is created, one of its attribu whether it is joinable or detached.
Only threads that are created as joinable If a thread is created as detached, it can joined.
To explicitly creat the attr argument used:
Declare a pthread attribute variable type
Initialize the attribute variable with pthread
Set the attribute detached status with pthread_attr_setdetachstate()
e a thread as joinable or detached in the pthread_create() routine is
When done, free library resources used by t
pthread_attr_destroy()
can be joined. never be
the pthread_attr_t data
_attr_init()
void *BusyWork(v …pthr
ead_exit((void *
oid *null) {
int main (int argc, char
pthread_attr_t attr;
int rc, t;
void *status;
rc = pthread_join(thread[t], &st
printf(“Completed join with thread (long)status);
}pthread_exit(NULL);
/* Initialize and set thread detached attribute */
pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREA
…/* Free attribute and wait for the other thre
pthread_attr_destroy(&attr);
for(t=0; t