POSIX Threads

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 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 [ [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 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() 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 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

Example

An example of using Pthreads in C:


#include
#include
#include
#include

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 ", 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 ", 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.

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

ee 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] " by Daniel 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] " by Michael 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.

Игры ⚽ Нужно решить контрольную?

Look at other dictionaries:

  • POSIX Threads — POSIX Threads  стандарт POSIX реализации потоков (нитей) выполнения, определяющий API для создания и управления ими. Библиотеки, реализующие этот стандарт (и функции этого стандарта), обычно называются Pthreads (функции имеют приставку… …   Википедия

  • POSIX — (IPAEng|ˈpɒzɪks) or Portable Operating System Interface cite web | title = POSIX | url = http://standards.ieee.org/regauth/posix/ | work = Standards | publisher = IEEE] is the collective name of a family of related standards specified by the IEEE …   Wikipedia

  • POSIX — est le nom d une famille de standards définie depuis 1988 par l Institute of Electrical and Electronics Engineers et formellement désignée IEEE 1003. Ces standards ont émergé d un projet de standardisation des API des logiciels destinés à… …   Wikipédia en Français

  • Posix — est le nom d une famille de standards définie depuis 1988 par l IEEE et formellement désignée IEEE 1003. Ces standards ont émergé d un projet de standardisation des API des logiciels destinés à fonctionner sur des variantes du système d… …   Wikipédia en Français

  • POSIX — es el acrónimo de Portable Operating System Interface; la X viene de UNIX como seña de identidad de la API. El término fue sugerido por Richard Stallman en respuesta a la demanda de la IEEE, que buscaba un nombre fácil de recordar. Una traducción …   Wikipedia Español

  • Поток POSIX — POSIX Threads стандарт потоков (нитей) выполнения, определяющий API для создания и управления ими. Библиотеки, реализующие этот стандарт (и функции этого стандарта), обычно называются Pthreads (функции имеют приставку «pthread »). Хотя наиболее… …   Википедия

  • POSIX — ● ►en /po six/ np. sg. ►UNIX Portable Operating System for Computer Environment. Norme Unix de l IEEE, numérotée 1003.1 , et qui spécifie le noyau du système. La norme POSIX 1 b spécifie les noyaux temps réel. La 1 c spécifie les threads. C était …   Dictionnaire d'informatique francophone

  • Native POSIX Thread Library — Die Native POSIX Thread Library (NPTL) ist eine moderne Implementierung einer Threading Bibliothek für Linux. Sie wird in Verbindung mit der GNU C Library (glibc) verwendet und erlaubt Linux Programmen die Verwendung von POSIX Threads (pthreads) …   Deutsch Wikipedia

  • Native POSIX Thread Library — The Native POSIX Thread Library (NPTL) is a software feature that enables the Linux kernel to run programs written to use POSIX Threads efficiently. Contents 1 History 2 Design 3 See also 4 References …   Wikipedia

  • GNU Portable Threads — Infobox Software name = GNU Portable Threads developer = Ralf S. Engelschall latest release version = 2.0.7 latest release date = 8 June 2006 operating system = UNIX genre = Runtime library license = LGPL website =… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”