Functions
|
|
Arguments:
- thread - returns the thread id. (unsigned long int defined in bits/pthreadtypes.h)
- attr - Set to NULL if default thread attributes are used. (else define members of the struct pthread_attr_t defined in bits/pthreadtypes.h) Attributes include:
- detached state (joinable? Default: PTHREAD_CREATE_JOINABLE. Other option: PTHREAD_CREATE_DETACHED)
- scheduling policy (real-time? PTHREAD_INHERIT_SCHED,PTHREAD_EXPLICIT_SCHED,SCHED_OTHER)
- scheduling parameter
- inheritsched attribute (Default: PTHREAD_EXPLICIT_SCHED Inherit from parent thread: PTHREAD_INHERIT_SCHED)
- scope (Kernel threads: PTHREAD_SCOPE_SYSTEM User threads: PTHREAD_SCOPE_PROCESS Pick one or the other not both.)
- guard size
- stack address (See unistd.h and bits/posix_opt.h _POSIX_THREAD_ATTR_STACKADDR)
- stack size (default minimum PTHREAD_STACK_SIZE set in pthread.h),
- void * (*start_routine) - pointer to the function to be threaded. Function has a single argument: pointer to void.
- *arg - pointer to argument of function. To pass multiple arguments, send a pointer to a structure.
|
|
Arguments:
- retval - Return value of thread.
This routine kills the thread. The pthread_exit function never returns. If the thread is not detached, the thread id and return value may be examined from another thread by using pthread_join.
|
|
The threads library provides three synchronization mechanisms:
- mutexes - Mutual exclusion lock: Block access to variables by other threads. This enforces exclusive access by a thread to a variable or set of variables.
- joins - Make a thread wait till others are complete (terminated**.
- condition variables - data type pthread_cond_t
Muteses:
|
|
|
|
Joins:
|
|
Condition Variables:
Functions used in conjunction with the condition variable:
- Creating/Destroying:
- pthread_cond_init
- pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
- pthread_cond_destroy
- Waiting on condition:
- pthread_cond_wait
- pthread_cond_timedwait - place limit on how long it will block.
- Waking thread based on condition:
- pthread_cond_signal
- pthread_cond_broadcast - wake up all threads blocked by the specified condition variable.
|
|
Example
Thread Creation and Termination
|
|
Single parameter
|
|
multiple parameters
|
|
Returning result
|
|
MPI+Pthread
在同一个进程内,不同的thread可以进行MPI的通信
|
|