POSIX Threads
From Wikipedia, the free encyclopedia
POSIX Threads is a POSIX standard for threads. The standard defines an API for creating and manipulating threads.
Libraries implementing the POSIX Threads standard are often named Pthreads. Pthreads are most commonly used on Unix-like POSIX systems such as Linux and Solaris, but Microsoft Windows implementations also exist. For example, the pthreads-w32 is available and supports a subset of the Pthread API [1]. (Note: in text, Pthreads is written with an upper-case P.[citation needed])
Contents |
Pthreads defines a set of C programming language types and procedure calls. It is implemented with a pthread.h header and a thread library.
Data types
- pthread_t: handle to a thread
- pthread_attr_t: thread attributes
Thread manipulation functions (arguments omitted for brevity):
- pthread_create(): create a thread
- pthread_exit(): terminate current thread
- pthread_cancel(): cancel execution of another thread
- pthread_join(): block current thread until another one terminates
- pthread_attr_init(): initialize thread attributes
- pthread_attr_setdetachstate(): set the detachstate attribute (whether thread can be joined on termination)
- pthread_attr_getdetachstate(): get the detachstate attribute
- pthread_attr_destroy(): destroy thread attributes
- pthread_kill(): send a signal to a thread
Synchronization functions: for mutexes and condition variables
- pthread_mutex_init()
- pthread_mutex_destroy()
- pthread_mutex_lock(): acquire mutex lock (blocking)
- pthread_mutex_trylock(): acquire mutex lock (non-blocking)
- pthread_mutex_unlock(): release mutex lock
- pthread_cond_init()
- pthread_cond_destroy()
- pthread_cond_signal(): signal a condition
- pthread_cond_wait(): wait on a condition
An example of using Pthreads in C:
#include#include #include #include void *thread_func( void *vptr_args ); void do_some_work( void ){ time_t start_time = time(NULL); while (time(NULL) == start_time); /* busy-wait for 0-1 seconds */ } int main( void ){ int i, j; pthread_t thread; pthread_create( &thread, NULL, &thread_func, NULL ); for( j= 0; j < 20; ++j ){ fprintf( stdout, "a\n" ); do_some_work(); } pthread_join( thread, NULL ); exit( EXIT_SUCCESS ); } void *thread_func( void *vptr_args ){ int i, j; for( j= 0; j < 20; ++j ){ fprintf( stderr, " b\n" ); do_some_work(); } return NULL; }
This program creates a new thread that prints lines containing 'b', while the main thread prints lines containing 'a'. The output is interleaved between 'a' and 'b' as a result of execution switching between the two threads. More tutorials can be found below in the links section.
- David R. Butenhof: Programming with POSIX Threads, Addison-Wesley, ISBN 0-201-63392-2
- Bradford Nichols, Dick Buttlar, Jacqueline Proulx Farell: Pthreads Programming, O'Reilly & Associates, ISBN 1-56592-115-1
- Charles J. Northrup: Programming with UNIX Threads, John Wiley & Sons, ISBN 0-471-13751-0
- POSIX Threading and Synchronous Wrappers
- Multithreaded Programming (Pthreads Tutorial)
- Pthreads Tutorial
- C/C++ Tutorial: using Pthreads
- Article "POSIX threads explained" by Daniel Robbins (Gentoo Linux founder)
- Interview "Ten Questions with David Butenhof about Parallel Programming and POSIX Threads" by Michael Suess
- Open Source POSIX Threads for Win32
- The Open Group Base Specifications Issue 6, IEEE Std 1003.1
- GNU Portable threads
|
|
|
|---|---|
| General | |
| Parallelism | |
| Theory |
Speedup · Amdahl's law · Flynn's taxonomy (SISD • SIMD • MISD • MIMD) · Cost efficiency · Gustafson's law · Karp-Flatt metric |
| Elements | |
| Coordination | |
| Programming | |
| Hardware |
Computer cluster · Beowulf · Symmetric multiprocessing · Non-Uniform Memory Access · Cache only memory architecture · Asymmetric multiprocessing · Simultaneous multithreading · Shared memory · Distributed memory · Massively parallel processing · Superscalar processing · Vector processing · Supercomputer · Stream processing · GPGPU |
| Software | |
| APIs |
POSIX Threads · OpenMP · Message Passing Interface (MPI) |
| Problems | |