| SP Parallel Programming II Workshop |
| p o s i x t h r e a d s p r o g r a m m i n g |
| Pthreads Overview |
|
| What Is A Thread? | |


| Pthreads Overview |
|
| What Are Pthreads? | |
| Pthreads Overview |
|
| Why Pthreads? | |
For example, the following table compares timing results for the fork() subroutine and the pthreads_create() subroutine. Timings reflect 50,000 process/thread creations, were performed with the timex utility, and units are in seconds.
| Architecture | fork()
| pthread_create()
| real
| user
| sys
| real
| user
| sys
|
| 92.42
| 2.66
| 105.29
| 8.72
| 4.97
| 3.93
|
| 259.21
| 8.84
| 249.26
| 28.79
| 17.82
| 21.03
|
| 55.54
| 2.31
| 43.49
| 5.05
| 2.54
| 2.41
| | ||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Pthreads Overview |
|
| Designing Threaded Programs | |

| The Pthreads API |
|
| Note: IBM's API includes a Fortran interface which may be used for convenience at the price of portability. |
Example
IBM Fortran Pthreads program
| Routine Prefix | Functional Group |
|---|---|
| pthread_ | Threads themselves and miscellaneous subroutines |
| pthread_attr | Thread attributes objects |
| pthread_mutex | Mutexes |
| pthread_mutexattr | Mutex attributes objects. |
| pthread_cond | Condition variables |
| pthread_condattr | Condition attributes objects |
| pthread_key | Thread-specific data keys |
| Thread Management |
|
| Creating Threads | |
| pthread_create (thread,attr,start_routine,arg) |
| Check your local implementation for limits on thread creation. For example, AIX 4.2 specifies a maximum of 512 threads per process with a default thread stack size of 56K bytes. |
| Thread Management |
|
| Terminating Thread Execution | |
| pthread_exit (status) |
| Thread Management |
|
| Example: Pthread Creation and Termination | |
The simple example code creates 5 threads with the pthread_create() routine. Each thread prints a "Hello World!" message, and then terminates with a call to pthread_exit().
| Thread Management |
|
| Passing Arguments To Threads | |
| Important: threads initially access their data structures in the parent thread's memory space. That data structure must not be corrupted/modified until the thread has finished accessing it. |
|
|
int rc, t;
for(t=0;t < NUM_THREADS;t++)
{
printf("Creating thread %d\n", t);
rc = pthread_create(&threads[t], NULL, PrintHello,
(void *) &t);
...
}
|
| Thread Management |
|
| Thread Identifiers | |
|
pthread_self ()
pthread_equal (thread1,thread2) |
| Thread Management |
|
| Joining Threads | |
| pthread_join (threadid,status) |
| Thread Management |
|
| Detaching / Undetaching Threads | |
|
pthread_detach (threadid,status)
pthread_attr_init (attr) pthread_attr_setdetachstate (attr,detachstate) pthread_attr_getdetachstate (attr,detachstate) pthread_attr_destroy (attr) |
| Current implementations differ in the default detached status at thread creation time. SGI for example, creates threads as joinable (undetached). IBM's AIX 4.2 implementation creates threads as unjoinable (detached) by default, whereas AIX 4.3 creates threads as joinable (undetached). The final standard specifies undetached as the default. |
| Thread Management |
|
| Example: Pthread Joining | |
| Mutex Variables |
|
| Overview | |
| Thread 1 | Thread 2 | Balance |
|---|---|---|
| Read balance: $1000 | $1000 | |
| Read balance: $1000 | $1000 | |
| Deposit $200 | $1000 | |
| Deposit $200 | $1000 | |
| Update balance $1000+$200 | $1200 | |
| Update balance $1000+$200 | $1200 |
| Mutex Variables |
|
| Creating / Destroying Mutexes | |
|
pthread_mutex_init (mutex,attr)
pthread_mutex_destroy (mutex) pthread_mutexattr_init (attr) pthread_mutexattr_destroy (attr) |
| Note: Your implementation may/may not provide the three optional mutex attributes. For example, in AIX 4.2 and 4.3 mutex attributes are not defined. |
| Mutex Variables |
|
| Locking / Unlocking Mutexes | |
|
pthread_mutex_lock (mutex)
pthread_mutex_trylock (mutex) pthread_mutex_unlock (mutex) |
| Mutex Variables |
|
| Example: Using Mutexes | |
| Condition Variables |
|
| Overview | |
| Calling Thread | |
|---|---|
| Declare and initialize global data/variables which require synchronization (such as "count") | |
| Declare and initialize a condition variable object | |
| Declare and initialize an associated mutex | |
| Create threads A and B to do work | |
| Thread A | Thread B |
|
|
| Join / Continue | |
| Condition Variables |
|
| Creating / Destroying Condition Variables | |
|
pthread_cond_init (condition,attr)
pthread_cond_destroy (condition) pthread_condattr_init (attr) pthread_condattr_destroy (attr) |
| Check your implementation for the ability to use the condition variable attributes object. For example, in AIX 4.2 and 4.3 this option is not implemented and the value "NULL" should be used. |
| Condition Variables |
|
| Waiting / Signalling On Condition Variables | |
|
pthread_cond_wait (condition,mutex)
pthread_cond_signal (condition) pthread_cond_broadcast (condition) |
| Condition Variables |
|
| Example: Using Condition Variables | |
This simple example code demonstrates the use of several Pthread condition variable routines. The main routine creates three threads. Two of the threads perform work and update a "count" variable. The third thread waits until the count variable reaches a specified value.
| Pthreads, MPI, SMPs, AIX and IBM's PE |
|
| Compiler Command | Description |
|---|---|
|
xlc_r cc_r | C pthreads compiler with default language level of ANSI |
| xlf_r | Fortran compiler with IBM Pthreads API (non-portable). |
| xlf90_r | Fortran 90 pthreads compiler |
| xlC_r | C++ pthreads compiler |
| mpcc_r | C MPI-pthreads compiler script |
| mpxlf_r | Fortran MPI-pthreads compiler script |
| mpCC_r | C++ MPI-pthreads compiler script |
| Architecture | MPI
| pthreads
|
| 22.54
| 0.11
|
| 92.14
| 0.31
| |
|---|
| Pthread Library Routines Reference |
|
| Pthread Functions | |
| Thread Management | pthread_create |
| pthread_exit | |
| pthread_join | |
| pthread_once | |
| pthread_kill | |
| pthread_self | |
| pthread_equal | |
| pthread_yield | |
| pthread_detach | |
| Thread Specific Data | pthread_key_create |
| pthread_key_delete | |
| pthread_getspecific | |
| pthread_setspecific | |
| Thread Cancellation | pthread_cancel |
| pthread_cleanup_pop | |
| pthread_cleanup_push | |
| pthread_setcancelstate | |
| pthread_getcancelstate | |
| pthread_testcancel | |
| Thread Scheduling | pthread_getschedparam |
| pthread_setschedparam | |
| Signals | pthread_sigmask |
| Pthread Attribute Functions | |
| Basic Management | pthread_attr_init |
| pthread_attr_destroy | |
| Detachable or Joinable | pthread_attr_setdetachstate |
| pthread_attr_getdetachstate | |
| Specifying Stack Information | pthread_attr_getstackaddr |
| pthread_attr_getstacksize | |
| pthread_attr_setstackaddr | |
| pthread_attr_setstacksize | |
| Thread Scheduling Attributes | pthread_attr_getschedparam |
| pthread_attr_setschedparam | |
| pthread_attr_getschedpolicy | |
| pthread_attr_setschedpolicy | |
| pthread_attr_setinheritsched | |
| pthread_attr_getinheritsched | |
| pthread_attr_setscope | |
| pthread_attr_getscope | |
| Mutex Functions | |
| Mutex Management | pthread_mutex_init |
| pthread_mutex_destroy | |
| pthread_mutex_lock | |
| pthread_mutex_unlock | |
| pthread_mutex_trylock | |
| Priority Management | pthread_mutex_setprioceiling |
| pthread_mutex_getprioceiling | |
| Mutex Attribute Functions | |
| Basic Management | pthread_mutexattr_init |
| pthread_mutexattr_destroy | |
| Sharing | pthread_mutexattr_getpshared |
| pthread_mutexattr_setpshared | |
| Protocol Attributes | pthread_mutexattr_getprotocol |
| pthread_mutexattr_setprotocol | |
| Priority Management | pthread_mutexattr_setprioceiling |
| pthread_mutexattr_getprioceiling | |
| Condition Variable Functions | |
| Basic Management | pthread_cond_init |
| pthread_cond_destroy | |
| pthread_cond_signal | |
| pthread_cond_broadcast | |
| pthread_cond_wait | |
| pthread_cond_timedwait | |
| Condition Variable Attribute Functions | |
| Basic Management | pthread_condattr_init |
| pthread_condattr_destroy | |
| Sharing | pthread_condattr_getpshared |
| pthread_condattr_setpshared | |
| References and More Information |
|