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 |
[edit] 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
Thread-local storage (or thread-specific data, in Pthreads nomenclature):
- pthread_key_create(): creates a key that can later be associated with thread specific data
- pthread_setspecific(): associate a key with thread-specific data
- pthread_getspecific(): retrieve the data associated with a key
- pthread_key_delete(): destroy the key
Some utility functions useful when working with Pthreads
- pthread_equal(): test two threads IDs for equality
- pthread_detach(): set thread to release resources
- pthread_self(): find out own thread ID
[edit] Example
An example of using Pthreads in C:
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <pthread.h> static void wait(void) { time_t start_time = time(NULL); while (time(NULL) == start_time) { /* do nothing except chew CPU slices for up to one second */ } } static void *thread_func(void *vptr_args) { int i; for (i = 0; i < 20; i++) { fputs(" b\n", stderr); wait(); } return NULL; } int main(void) { int i; pthread_t thread; if (pthread_create(&thread, NULL, thread_func, NULL) != 0) { return EXIT_FAILURE; } for (i = 0; i < 20; i++) { fputs("a\n", stdout); wait(); } if (pthread_join(thread, NULL) != 0) { return EXIT_FAILURE; } return EXIT_SUCCESS; }
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.
[edit] References
- 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
- Kay A. Robbins and Steven Robbins, UNIX Systems Programming, Prentice-Hall, ISBN 0-13-042411-0
[edit] See also
- Native POSIX Thread Library (NPTL)
- Spurious wakeup
- Thread-local storage
- GNU Portable Threads
- FSU Pthreads
[edit] External links
- 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
- Pthreads Presentation at 2007 OSCON (O'Reilly Open Source Convention) by Adrien Lamothe. An overview of Pthreads with current trends.
|
||||||||||||||||||||||||||||||||

