HOME

TheInfoList



OR:

POSIX Threads, commonly known as pthreads, is an
execution model A programming language consists of a grammar/syntax plus an execution model. The execution model specifies the behavior of elements of the language. By applying the execution model, one can derive the behavior of a program that was written in term ...
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 interf ...
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 operation ...
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 FreeBSD is a free and open-source Unix-like operating system descended from the Berkeley Software Distribution (BSD), which was based on Research Unix. The first version of FreeBSD was released in 1993. In 2005, FreeBSD was the most popular ...
,
NetBSD NetBSD is a free and open-source Unix operating system based on the Berkeley Software Distribution (BSD). It was the first open-source BSD descendant officially released after 386BSD was forked. It continues to be actively developed and is a ...
,
OpenBSD OpenBSD is a security-focused, free and open-source, Unix-like operating system based on the Berkeley Software Distribution (BSD). Theo de Raadt created OpenBSD in 1995 by forking NetBSD 1.0. According to the website, the OpenBSD project em ...
,
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, which ...
,
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 Solaris may refer to: Arts and entertainment Literature, television and film * ''Solaris'' (novel), a 1961 science fiction novel by Stanisław Lem ** ''Solaris'' (1968 film), directed by Boris Nirenburg ** ''Solaris'' (1972 film), directed by ...
,
Redox Redox (reduction–oxidation, , ) is a type of chemical reaction in which the oxidation states of substrate (chemistry), substrate change. Oxidation is the loss of Electron, electrons or an increase in the oxidation state, while reduction ...
, and AUTOSAR Adaptive, typically bundled as a library libpthread.
DR-DOS DR-DOS (written as DR DOS, without a hyphen, in versions up to and including 6.0) is a disk operating system for IBM PC compatibles. Upon its introduction in 1988, it was the first DOS attempting to be compatible with IBM PC DOS and MS-D ...
and
Microsoft Windows Windows is a group of several proprietary graphical operating system families developed and marketed by Microsoft. Each family caters to a certain sector of the computing industry. For example, Windows NT for consumers, Windows Server for serv ...
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, collection of values used for computations. * File type * TYPE (DOS command), a command to display contents of a file. * Typ ...
, 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 variable In concurrent programming, a monitor is a synchronization construct that allows threads to have both mutual exclusion and the ability to wait (block) for a certain condition to become false. Monitors also have a mechanism for signaling other th ...
s *
Synchronization Synchronization is the coordination of events to operate a system in unison. For example, the conductor of an orchestra keeps the orchestra synchronized or ''in time''. Systems that operate with all parts in synchrony are said to be synchronou ...
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, and ot ...
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 Interix was an optional, POSIX-conformant Unix subsystem for Windows NT operating systems. Interix was a component of Windows Services for UNIX, and a superset of the Microsoft POSIX subsystem. Like the POSIX subsystem, Interix was an environment ...
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 syste ...
*
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 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 ru ...
(TBB) *
Native POSIX Thread Library The Native POSIX Thread Library (NPTL) is an implementation of the POSIX Threads specification for the Linux operating system. History Before the 2.6 version of the Linux kernel, processes were the schedulable entities, and there were no special f ...
(NPTL) *
DCEThreads DCEThreads is an implementation of POSIX Draft 4 threads. DCE Stands for "Distributed Computing Environment" DCEThreads allowed users to create multiple avenues of execution in a single process. It is based on pthreads interface. History DCE/RPC ...
*
clone (Linux system call) In computing, particularly in the context of the Unix operating system and its workalikes, fork is an operation whereby a process creates a copy of itself. It is an interface which is required for compliance with the POSIX and Single UNIX Speci ...
* Spurious wakeup *
Thread-local storage Thread-local storage (TLS) is a computer programming method that uses static or global memory local to a thread. While the use of global variables is generally discouraged in modern programming, legacy operating systems such as UNIX are designed ...
*
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 paralleli ...
(Apple's thread library) *
Beginthread The beginthread function creates a new thread of execution within the current process. It is part of the Microsoft Windows runtime library and is declared in the process.h header file. Prototype unsigned long _beginthread(void(* Func)(void*), ...
(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)