Unformatted text preview:

COSC 6374 Parallel Computation Shared memory programming with POSIX Threads Edgar Gabriel Fall 2011 Edgar Gabriel References Some of the slides in this lecture is based on the following references http www cobweb ecn purdue edu eigenman ECE563 H andouts pthreads ppt W Richard Stevens Stephen A Rago Advanced Programming in the UNIX environment Section 11 AddisonWesley Professional Computing Series Rolf Rabenseifner Georg Hager Gabriele Jost Rainer Keller Hybrid MPI and OpenMP Parallel Programming Tutorial S10 at Supercomputing 2007 Reno Nevada USA http www yolinux com TUTORIALS LinuxTutorialPosixThre ads html Edgar Gabriel 1 POSIX Threads Overview Shared memory programming model POSIX threads pthreads programming model creation of threads managing thread execution managing the shared resources of the process IEEE s POSIX Threads Model programming models for threads in a UNIX platform pthreads are included in the international standards ISO IEC9945 1 Edgar Gabriel Execution model Main thread initial thread created when main in C are invoked by the process loader once in the main the application can create additional threads if the main thread returns the process terminates even if there are running threads in that process unless special precautions are taken to explicitly avoid terminating the entire process use pthread exit Edgar Gabriel 2 Simple Example I include pthread h int main int argc char argv int threadid ret main thread spawns another thread ret pthread create threadid NULL tfunc NULL if ret 0 printf Error creating a new thread n return 0 void tfunc void arg pid t pid pthread t tid pid getpid determine the process id tid pthread self determine the thread id return NULL Edgar Gabriel pthread create pthread create pthread t tidp const pthread attr t attr void start rtn func void arg tidp upon completion of the function set to the thread id of the new thread attr argument used to customize the new thread func function pointer to be executed by the new thread The prototype of the function has to be void func void arg arg argument to be passed to the function func Edgar Gabriel 3 pthread self and getpid pid t getpid void returns the process id of the calling process all threads that have been spawned from a process have the same process id pthread t pthread self void returns the thread id of the calling thread not necessarily unique on a system only within the calling process Edgar Gabriel Thread termination If any thread within a process calls exit the entire process is terminated To terminate a thread without effecting the other threads return from the function Return value is the exit code of the thread a thread can request that in the same process another thread is cancelled using int pthread cancel pthread t threadid typically done by the main thread return 0 if ok error number on failure Edgar Gabriel 4 Thread termination II A thread can call int pthread exit void rval ptr rval ptr single argument to pass to the counter part can also be a pointer to a structure int pthread join pthread t threadid void rval ptr typically called by the master thread rval ptr will contain the value passed by the terminating thread to pthread exit Edgar Gabriel Simple Example II include pthread h int main int argc char argv int threadid ret int val main thread spawns another thread ret pthread create threadid NULL tfunc NULL if ret 0 printf Error creating a new thread n do something pthread join threadid val return 0 void tfunc void arg do something useful pthread exit void 1 return NULL Edgar Gabriel 5 Thread synchronization Thread A Thread B read time write1 read write2 Reading and writing a shared variable between two threads Timing between two threads will differ in every iteration If you need a specific value for thread B of the variable you need to synchronize access to the shared variable Edgar Gabriel Thread synchronization Three methods discussed here Mutex locks Condition variables Reader Writer locks Edgar Gabriel 6 Mutex locks Mutex Mutual exclusion a lock is used before accessing a shared resource and released after the access mutex lock represented by a mutex variable while mutex lock is set other threads that try to access the lock will be denied if more than one thread wait for the lock all of them will be made runnable but only one thread will get the lock All threads have to use mutex locks for accessing the variable else no guarantee on correctness Edgar Gabriel Mutex locks II int pthread mutex init pthread mutex r mutex const pthread mutexattr t attr int pthread mutex destroy pthread mutex t mutex mutex mutex variable to be initialized destroyed counter part once initialized a mutex variable can be used for an unlimited number of lock unlock operations attr attributes for the mutex Edgar Gabriel 7 Mutex locks III int pthread mutex lock pthread mutex t mutex int pthread mutex trylock pthread mutex t mutex int pthread mutex unlock pthread mutex t mutex pthread mutex lock acquire lock for the mutex If mutex is already blocked by another thread wait until the mutex is unlocked pthread mutex trylock acquire lock for the mutex If mutex is already blocked by another thread do not wait but return EBUSY to indicated failure Edgar Gabriel Thread synchronization revisited Thread A Thread B read time write1 write2 read Example Force thread B to read value of shared variable after write2 Edgar Gabriel 8 Simple Example IIIa include pthread h int value 0 pthread mutex t mymutex shared variable mutex variable int main int argc char argv int threadid ret main thread spawns another thread ret pthread create threadid NULL tfunc NULL if ret 0 printf Error creating a thread n pthread mutex init mymutex NULL pthread mutex lock mymutex value 1 value pthread mutex unlock mymutex Initialize mutex Acquire mutex lock write 1 write 2 Release lock pthread join threadid val pthread mutex destroy mymutex Edgar Gabriel return 0 wait for other thread destroy mutex Simple Example IIIb void tfunc void arg int localvalue pthread mutex lock mymutex localvalue value pthread mutex unlock mymutex wait for lock read shared variable pthread exit void 1 return NULL Edgar Gabriel 9 Mutex locks IV A thread will deadlock itself if it tries to lock the same mutex twice If more than one mutex is used a deadlock can occur if one thread holds lock1 and waits for lock2 and the other thread holds lock2 and waits for lock1 Order for accessing mutexes has to be identical in all code pathes e g need to hold lock1 in order


View Full Document

UH COSC 6374 - Shared memory programming with POSIX Threads

Documents in this Course
Load more
Loading Unlocking...
Login

Join to view Shared memory programming with POSIX Threads and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Shared memory programming with POSIX Threads and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?