- POSIX Threads
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 asLinux and Solaris, butMicrosoft Windows implementations also exist. For example, the pthreads-w32 is available and supports a subset of the Pthread API [ [http://sources.redhat.com/pthreads-win32/ Pthread Win-32] ] . (Note: in text, Pthreads is written with an upper-case P.)Contents
Pthreads defines a set of C programming language types, functions and constants. It is implemented with a [http://opengroup.org/onlinepubs/007908799/xsh/pthread.h.html pthread.h] header and a thread library.
Data types
* pthread_t: handle to a thread
* pthread_attr_t: thread attributesThread 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 threadSynchronization functions: for
mutex es and condition variables
* pthread_mutex_init() initialize mutex lock
* 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 conditionThread-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 keySome 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 IDExample
An example of using Pthreads in C:
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.
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 andSteven Robbins , "UNIX Systems Programming", Prentice-Hall, ISBN 0-13-042411-0ee also
*
Native POSIX Thread Library (NPTL)
*Spurious wakeup
*Thread-local storage
*GNU Portable Threads
*FSU Pthreads External links
* [http://sources.redhat.com/pthreads-win32/ Pthread Win-32] , Basic Programming
* [http://www.llnl.gov/computing/tutorials/pthreads/ Pthreads Tutorial]
* [http://yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html C/C++ Tutorial: using Pthreads]
* Article " [http://www-128.ibm.com/developerworks/linux/library/l-posix1.html POSIX threads explained] " byDaniel Robbins (Gentoo Linux founder)
* Interview " [http://www.thinkingparallel.com/2007/04/11/ten-questions-with-david-butenhof-about-parallel-programming-and-posix-threads/ Ten Questions with David Butenhof about Parallel Programming and POSIX Threads] " byMichael Suess
* [http://sources.redhat.com/pthreads-win32/ Open Source POSIX Threads for Win32]
* [http://www.opengroup.org/onlinepubs/007904975/basedefs/pthread.h.html The Open Group Base Specifications Issue 6, IEEE Std 1003.1]
* [http://www.gnu.org/software/pth/ GNU Portable threads]
* [http://conferences.oreillynet.com/presentations/os2007/os_lamothe.pdf Pthreads Presentation at 2007 OSCON (O'Reilly Open Source Convention) by Adrien Lamothe. An overview of Pthreads with current trends.]
Wikimedia Foundation. 2010.