HOME

TheInfoList



OR:

In
computing Computing is any goal-oriented activity requiring, benefiting from, or creating computer, computing machinery. It includes the study and experimentation of algorithmic processes, and the development of both computer hardware, hardware and softw ...
, POSIX Threads, commonly known as pthreads, is an execution model that exists independently from a
programming language A programming language is a system of notation for writing computer programs. Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
, as well as a parallel execution model. It allows a program to control multiple different flows of work that overlap in time. Each flow of work is referred to as a '' thread'', and creation and control over these flows is achieved by making calls to the POSIX Threads
API An application programming interface (API) is a connection between computers or between computer programs. It is a type of software interface, offering a service to other pieces of software. A document or standard that describes how to build ...
. POSIX Threads is an API defined by the
Institute of Electrical and Electronics Engineers The Institute of Electrical and Electronics Engineers (IEEE) is an American 501(c)(3) public charity professional organization for electrical engineering, electronics engineering, and other related disciplines. The IEEE has a corporate office ...
(IEEE) standard ''
POSIX The Portable Operating System Interface (POSIX; ) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines application programming interfaces (APIs), along with comm ...
.1c, Threads extensions (IEEE Std 1003.1c-1995)''. Implementations of the API are available on many
Unix-like A Unix-like (sometimes referred to as UN*X, *nix or *NIX) operating system is one that behaves in a manner similar to a Unix system, although not necessarily conforming to or being certified to any version of the Single UNIX Specification. A Uni ...
POSIX-conformant
operating system An operating system (OS) is system software that manages computer hardware and software resources, and provides common daemon (computing), services for computer programs. Time-sharing operating systems scheduler (computing), schedule tasks for ...
s such as
FreeBSD FreeBSD is a free-software Unix-like operating system descended from the Berkeley Software Distribution (BSD). The first version was released in 1993 developed from 386BSD, one of the first fully functional and free Unix clones on affordable ...
,
NetBSD NetBSD is a free and open-source Unix-like operating system based on the Berkeley Software Distribution (BSD). It was the first open-source BSD descendant officially released after 386BSD was fork (software development), forked. It continues to ...
,
OpenBSD OpenBSD is a security-focused operating system, security-focused, free software, Unix-like operating system based on the Berkeley Software Distribution (BSD). Theo de Raadt created OpenBSD in 1995 by fork (software development), forking NetBSD ...
,
Linux Linux ( ) is a family of open source Unix-like operating systems based on the Linux kernel, an kernel (operating system), operating system kernel first released on September 17, 1991, by Linus Torvalds. Linux is typically package manager, pac ...
,
macOS macOS, previously OS X and originally Mac OS X, is a Unix, Unix-based operating system developed and marketed by Apple Inc., Apple since 2001. It is the current operating system for Apple's Mac (computer), Mac computers. With ...
, Android, Solaris,
Redox Redox ( , , reduction–oxidation or oxidation–reduction) is a type of chemical reaction in which the oxidation states of the reactants change. Oxidation is the loss of electrons or an increase in the oxidation state, while reduction is t ...
, and AUTOSAR Adaptive, typically bundled as a library libpthread.
DR-DOS DR-DOS is a disk operating system for IBM PC compatibles, originally developed by Gary A. Kildall's Digital Research, Inc. and derived from Concurrent PC DOS 6.0, which was an advanced successor of CP/M-86. Upon its introduction in 198 ...
and
Microsoft Windows Windows is a Product lining, product line of Proprietary software, proprietary graphical user interface, graphical operating systems developed and marketed by Microsoft. It is grouped into families and subfamilies that cater to particular sec ...
implementations also exist: within the SFU/SUA subsystem which provides a native implementation of a number of POSIX APIs, and also within
third-party Third party may refer to: Business * Third-party source, a supplier company not owned by the buyer or seller * Third-party beneficiary, a person who could sue on a contract, despite not being an active party * Third-party insurance, such as a veh ...
packages such as ''pthreads-w32'', which implements pthreads on top of existing
Windows API The Windows API, informally WinAPI, is the foundational application programming interface (API) that allows a computer program to access the features of the Microsoft Windows operating system in which the program is running. Programs can acces ...
.


Contents

pthreads defines a set of C programming language
types Type may refer to: Science and technology Computing * Typing, producing text via a keyboard, typewriter, etc. * Data type, collection of values used for computations. * File type * TYPE (DOS command), a command to display contents of a file. * Ty ...
, functions and constants. It is implemented with a pthread.h header and a thread
library A library is a collection of Book, books, and possibly other Document, materials and Media (communication), media, that is accessible for use by its members and members of allied institutions. Libraries provide physical (hard copies) or electron ...
. There are around 100 threads procedures, all prefixed pthread_ and they can be categorized into five groups: *Thread management – creating, joining threads etc. * Mutexes * Condition variables *
Synchronization Synchronization is the coordination of events to operate a system in unison. For example, the Conductor (music), conductor of an orchestra keeps the orchestra synchronized or ''in time''. Systems that operate with all parts in synchrony are sa ...
between threads using read write locks and barriers * Spinlocks The POSIX
semaphore Semaphore (; ) is the use of an apparatus to create a visual signal transmitted over distance. A semaphore can be performed with devices including: fire, lights, flags, sunlight, and moving arms. Semaphores can be used for telegraphy when arra ...
API works with POSIX threads but is not part of the threads standard, having been defined in the ''POSIX.1b, Real-time extensions (IEEE Std 1003.1b-1993)'' standard. Consequently, the semaphore procedures are prefixed by sem_ instead of pthread_.


Example

An example illustrating the use of pthreads in C: #include #include #include #include #include #define NUM_THREADS 5 void *perform_work(void *arguments) int main(void) This program creates five threads, each executing the function ''perform_work'' that prints the unique number of this thread to standard output. If a programmer wanted the threads to communicate with each other, this would require defining a variable outside of the scope of any of the functions, making it a
global variable In computer programming, a global variable is a variable with global scope, meaning that it is visible (hence accessible) throughout the program, unless shadowed. The set of all global variables is known as the ''global environment'' or ''global ...
. This program can be compiled using the gcc compiler with the following command: gcc pthreads_demo.c -pthread -o pthreads_demo Here is one of the many possible outputs from running this program. In main: Creating thread 0. In main: Creating thread 1. In main: Creating thread 2. In main: Creating thread 3. Thread 0: Started. In main: Creating thread 4. Thread 3: Started. Thread 2: Started. Thread 0: Will be sleeping for 3 seconds. Thread 1: Started. Thread 1: Will be sleeping for 5 seconds. Thread 2: Will be sleeping for 4 seconds. Thread 4: Started. Thread 4: Will be sleeping for 1 seconds. In main: All threads are created. Thread 3: Will be sleeping for 4 seconds. Thread 4: Ended. Thread 0: Ended. In main: Thread 0 has ended. Thread 2: Ended. Thread 3: Ended. Thread 1: Ended. In main: Thread 1 has ended. In main: Thread 2 has ended. In main: Thread 3 has ended. In main: Thread 4 has ended. Main program has ended.


POSIX Threads for Windows

Windows does not support the pthreads standard natively, therefore the Pthreads4w project seeks to provide a portable and open-source wrapper implementation. It can also be used to port
Unix Unix (, ; trademarked as UNIX) is a family of multitasking, multi-user computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, a ...
software (which uses pthreads) with little or no modification to the Windows platform. Pthreads4w version 3.0.0 or later, released under the Apache Public License v2.0, is compatible with 64-bit or 32-bit Windows systems. Version 2.11.0, released under the LGPLv3 license, is also 64-bit or 32-bit compatible. The Mingw-w64 project also contains a wrapper implementation of 'pthreads, winpthreads, which tries to use more native system calls than the Pthreads4w project. Interix environment subsystem available in the Windows Services for UNIX/Subsystem for UNIX-based Applications package provides a native port of the pthreads API, i.e. not mapped on Win32 API but built directly on the operating system syscall interface.


See also

*
Runtime system In computer programming, a runtime system or runtime environment is a sub-system that exists in the computer where a program is created, as well as in the computers where the program is intended to be run. The name comes from the compile time ...
*
OpenMP OpenMP is an application programming interface (API) that supports multi-platform shared-memory multiprocessing programming in C, C++, and Fortran, on many platforms, instruction-set architectures and operating systems, including Solaris, ...
*
Cilk Cilk, Cilk++, Cilk Plus and OpenCilk are general-purpose programming languages designed for multithreaded parallel computing. They are based on the C and C++ programming languages, which they extend with constructs to express parallel loop ...
/ Cilk Plus *
Threading Building Blocks oneAPI Threading Building Blocks (oneTBB; formerly Threading Building Blocks or TBB) is a C++ template library developed by Intel for parallel programming on multi-core processors. Using TBB, a computation is broken down into tasks that can r ...
(TBB) * Native POSIX Thread Library (NPTL) * DCEThreads *
clone (Linux system call) In computing, particularly in the context of the Unix operating system and Unix-like, its workalikes, fork is an operation whereby a Computer process, process creates a copy of itself. It is an interface which is required for compliance with the P ...
* Spurious wakeup *
Thread-local storage In computer programming, thread-local storage (TLS) is a memory management method that uses static memory allocation, static or global computer storage, memory local to a thread (computing), thread. The concept allows storage of data that appear ...
*
GNU Portable Threads GNU Pth (Portable Threads) is a POSIX/ANSI- C based user space thread library for UNIX platforms that provides priority-based scheduling for multithreading applications. GNU Pth targets for a high degree of portability. It is part of the GNU Pr ...
*
Grand Central Dispatch Grand Central Dispatch (GCD or libdispatch) is a technology developed by Apple Inc. to optimize application support for systems with multi-core processors and other symmetric multiprocessing systems. It is an implementation of task parallelism ...
(Apple's thread library) * Beginthread (a subroutine within Windows for creating a new thread and Unix thread) * State Threads, an event driven approach to threading


References


Further reading

* * * *


External links


The Open Group Base Specifications Issue 7, IEEE Std 1003.1
{{DEFAULTSORT:Posix Threads C POSIX library Parallel computing Threads (computing)