POSIX Threads

POSIX Threads, usually referred to 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 Threads is an API defined by the standard POSIX.1c, Threads extensions (IEEE Std 1003.1c-1995). Implementations of the API are available on many Unix-like POSIX-conformant operating systems such as FreeBSD, NetBSD, OpenBSD, Linux, macOS, Android,[1] Solaris, Redox, 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 packages such as pthreads-w32,[2] which implements pthreads on top of existing Windows API.

## Contents

* 1 Contents
* 2 Example
* 3 POSIX Threads for Windows
* 4 See also
* 5 References
* 6 Further reading
* 7 External links

## Contents

pthreads defines a set of C programming language types, functions and constants. It is implemented with a `pthread.h` header and a thread library.

There are around 100 threads procedures, all prefixed `pthread_` and they can be categorized into four groups:

* Thread management - creating, joining threads etc.
* Mutexes
* Condition variables
* Synchronization between threads using read/write locks and barriers

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 <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <pthread.h>
#include <unistd.h>

#define NUM_THREADS 5

void *perform_work(void *arguments){
int index = *((int *)arguments);
int sleep_time = 1 + rand() % NUM_THREADS;
printf("THREAD %d: Started.\n", index);
printf("THREAD %d: Will be sleeping for %d seconds.\n", index, sleep_time);
sleep(sleep_time);
printf("THREAD %d: Ended.\n", index);

int main(void) {
pthread_t threads[NUM_THREADS];
int thread_args[NUM_THREADS];
int i;
int result_code;

//create all threads one by one
for (i = 0; i < NUM_THREADS; i++) {
printf("IN MAIN: Creating thread %d.\n", i);
thread_args[i] = i;
result_code = pthread_create(&threads[i], NULL, perform_work, &thread_args[i]);
assert(!result_code);

printf("IN MAIN: All threads are created.\n");

//wait for each thread to complete
for (i = 0; i < NUM_THREADS; i++) {
result_code = pthread_join(threads[i], NULL);
assert(!result_code);
printf("IN MAIN: Thread %d has ended.\n", i);

printf("MAIN program has ended.\n");
return 0;

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. This program can be compiled using the gcc compiler with the following command:

gcc pthreads_demo.c -lpthread -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 software (which uses pthreads) with little or no modification to the Windows platform.[3] Pthreads4w version 3.0.0[4] or later, released under the Apache Public License v2.0, is compatible with 64-bit or 32-bit Windows systems. Version 2.11.0,[5] 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.[6]

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/Win64 API but built directly on the operating system syscall interface.[7]

## See also

* Runtime system
* OpenMP
* Cilk/Cilk Plus
* Threading Building Blocks (TBB)
* Native POSIX Thread Library (NPTL)
* DCEThreads
* clone (Linux system call)
* Spurious wakeup
* Thread-local storage
* GNU Portable Threads
* FSU Pthreads
* Grand Central Dispatch (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

1. ↑ "libc/bionic/pthread.c - platform/bionic - Git at Google". https://android.googlesource.com/platform/bionic/+/10ce969/libc/bionic/pthread.c.
2. ↑ "Pthread Win-32: Level of standards conformance". 2006-12-22. http://sources.redhat.com/pthreads-win32/conformance.html.
3. ↑ Hart, Johnson M. (2004-11-21). "Experiments with the Open Source Pthreads Library and Some Comments". http://world.std.com/~jmhart/opensource.htm.
4. ↑ https://sourceforge.net/projects/pthreads4w/files/ \-- File: pthreads4w-code-v3.0.0.zip -- Source for pthreads4w v3.0.0
5. ↑ https://sourceforge.net/projects/pthreads4w/files/ \-- File: pthreads4w-code-v2.11.0.zip -- Source for pthreads4w v2.11.0
6. ↑ see http://locklessinc.com/articles/pthreads_on_windows which is where it was originally derived from
7. ↑ "Chapter 1: Introduction to Windows Services for UNIX 3.5". https://technet.microsoft.com/en-us/library/bb496994.aspx.

## Further reading

* David R. Butenhof (1997). Programming with POSIX Threads. Addison-Wesley. ISBN 978-0-201-63392-4.
* Bradford Nichols; Dick Buttlar; Jacqueline Proulx Farell (September 1996). Pthreads Programming. O'Reilly & Associates. ISBN 978-1-56592-115-3. https://archive.org/details/pthreadsprogramm00nich.
* Charles J. Northrup (1996-01-25). Programming with UNIX Threads. John Wiley & Sons. ISBN 978-0-471-13751-1. https://archive.org/details/programmingwithu0000nort.
* Kay A. Robbins; Steven Robbins (2003). UNIX Systems Programming. Prentice-Hall. ISBN 978-0-13-042411-2. https://archive.org/details/unixsystemsprogr0000robb.

## External links

* The Open Group Base Specifications Issue 7, IEEE Std 1003.1

* v
* t
* e

Parallel computing

General|

* Distributed computing
* Parallel computing
* Massively parallel
* Cloud computing
* High-performance computing
* Multiprocessing
* Manycore processor
* GPGPU
* Computer network
* Systolic array

Levels|

* Bit
* Instruction
* Thread
* Task
* Data
* Memory
* Loop
* Pipeline

Multithreading|

* Temporal
* Simultaneous (SMT)
* Speculative (SpMT)
* Preemptive
* Cooperative
* Clustered Multi-Thread (CMT)
* Hardware scout

Theory|

* PRAM model
* PEM Model
* Analysis of parallel algorithms
* Amdahl's law
* Gustafson's law
* Cost efficiency
* Karp–Flatt metric
* Slowdown
* Speedup

Elements|

* Process
* Thread
* Fiber
* Instruction window
* Array data structure

Coordination|

* Multiprocessing
* Memory coherency
* Cache coherency
* Cache invalidation
* Barrier
* Synchronization
* Application checkpointing

Programming|

* Stream processing
* Dataflow programming
* Models
* Implicit parallelism
* Explicit parallelism
* Concurrency
* Non-blocking algorithm

Hardware|

* Flynn's taxonomy
* SISD
* SIMD
* SIMT
* MISD
* MIMD
* Dataflow architecture
* Pipelined processor
* Superscalar processor
* Vector processor
* Multiprocessor
* symmetric
* asymmetric
* Memory
* shared
* distributed
* distributed shared
* UMA
* NUMA
* COMA
* Massively parallel computer
* Computer cluster
* Grid computer
* Hardware acceleration

APIs|

* Ateji PX
* Boost.Thread
* Chapel
* HPX
* Charm++
* Cilk
* Coarray Fortran
* CUDA
* HIP
* Dryad
* C++ AMP
* Global Arrays
* MPI
* OpenMP
* OpenCL
* OpenHMPP
* OpenACC
* TPL
* PLINQ
* PVM
* POSIX Threads
* RaftLib
* UPC
* TBB
* ZPL

Problems|

* Automatic parallelization
* Deadlock
* Livelock
* Deterministic algorithm
* Embarrassingly parallel
* Parallel slowdown
* Race condition
* Software lockout
* Scalability
* Starvation

* Category: parallel computing
* Media related to Parallel computing at Wikimedia Commons

0.00

(0 votes)

|

Retrieved from "https://handwiki.org/wiki/index.php?title=POSIX_Threads&oldid=190494"
*[v]: View this template
*[t]: Discuss this template
*[e]: Edit this template