HOME

TheInfoList



OR:

POSIX Threads, commonly known as pthreads, is an execution model that exists independently from a language, 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.
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 both the system- and user-level application programming inter ...
Threads is an
API An application programming interface (API) is a way for two or more computer programs to communicate with each other. It is a type of software interface, offering a service to other pieces of software. A document or standard that describes how ...
defined by the standard ''POSIX.1c, Threads extensions (
IEEE The Institute of Electrical and Electronics Engineers (IEEE) is a 501(c)(3) professional association for electronic engineering and electrical engineering (and associated disciplines) with its corporate office in New York City and its operat ...
Std 1003.1c-1995)''. Implementations of the API are available on many
Unix-like A Unix-like (sometimes referred to as UN*X 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 Unix-li ...
POSIX-conformant operating systems such as FreeBSD, NetBSD, OpenBSD,
Linux Linux ( or ) is a family of open-source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991, by Linus Torvalds. Linux is typically packaged as a Linux distribution, w ...
,
macOS macOS (; previously OS X and originally Mac OS X) is a Unix operating system developed and marketed by Apple Inc. since 2001. It is the primary operating system for Apple's Mac computers. Within the market of desktop and lapt ...
, Android, Solaris,
Redox Redox (reduction–oxidation, , ) is a type of chemical reaction in which the oxidation states of substrate change. Oxidation is the loss of electrons or an increase in the oxidation state, while reduction is the gain of electrons or a ...
, and AUTOSAR Adaptive, typically bundled as a library libpthread. DR-DOS and Microsoft Windows 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 Ve ...
packages such as ''pthreads-w32'', which implements pthreads on top of existing
Windows API The Windows API, informally WinAPI, is Microsoft's core set of application programming interfaces (APIs) available in the Microsoft Windows operating systems. The name Windows API collectively refers to several different platform implementations th ...
.


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 In computer science and computer programming, a data type (or simply type) is a set of possible values and a set of allo ...
, functions and constants. It is implemented with a pthread.h header and a thread
library A library is a collection of materials, books or media that are accessible for use and not just for display purposes. A library provides physical (hard copies) or digital access (soft copies) materials, and may be a physical location or a vir ...
. There are around 100 threads procedures, all prefixed pthread_ and they can be categorized into four groups: *Thread management - creating, joining threads etc. *
Mutex In computer science, a lock or mutex (from mutual exclusion) is a synchronization primitive: a mechanism that enforces limits on access to a resource when there are many threads of execution. A lock is designed to enforce a mutual exclusion concur ...
es * Condition variables * Synchronization between threads using read write locks and barriers *
Spinlock In software engineering, a spinlock is a lock that causes a thread trying to acquire it to simply wait in a loop ("spin") while repeatedly checking whether the lock is available. Since the thread remains active but is not performing a useful task, ...
s The POSIX semaphore API works with POSIX threads but is not part of 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 s ...
. 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, multiuser 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, an ...
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 Mingw-w64 is a free and open source software development environment to create (cross-compile) Microsoft Windows PE applications. It was forked in 2005–2010 from MinGW (''Minimalist GNU for Windows''). Mingw-w64 includes a port of the GNU ...
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 In computing, a system call (commonly abbreviated to syscall) is the programmatic way in which a computer program requests a service from the operating system on which it is executed. This may include hardware-related services (for example, acc ...
interface.


See also

*
Runtime system In computer programming, a runtime system or runtime environment is a sub-system that exists both 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 t ...
*
OpenMP OpenMP (Open Multi-Processing) 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 syst ...
*
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 loops ...
/
Cilk Plus 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 ...
* Threading Building Blocks (TBB) * Native POSIX Thread Library (NPTL) * DCEThreads * clone (Linux system call) * Spurious wakeup * Thread-local storage * GNU Portable Threads *
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 paralleli ...
(Apple's thread library) * Beginthread (a subroutine within Windows for creating a new thread and unix thread) *
State Threads The State Threads library is a small application library which provides a foundation for writing fast and highly scalable Internet applications (such as web servers, proxy servers, mail transfer agents, or any network-data-driven application) on Un ...
, 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)