Incomputing,POSIX Threads, commonly known aspthreads (after its header<pthread.h>), is anexecution model that exists independently from aprogramming language, as well as aparallel execution model. It allows aprogram to control multiple different flows of work that overlap in time. Each flow of work is referred to as athread, and creation and control over these flows is achieved by making calls to the POSIX ThreadsAPI. POSIX Threads is an API defined by theInstitute of Electrical and Electronics Engineers (IEEE) standardPOSIX.1c, Threads extensions (IEEE Std 1003.1c-1995).
Implementations of the API are available on manyUnix-like POSIX-conformantoperating systems such asFreeBSD,NetBSD,OpenBSD,Linux,macOS,Android,[1]Solaris,Redox,QNX, andAUTOSAR Adaptive, typically bundled as a librarylibpthread.DR-DOS andMicrosoft Windows implementations also exist: within theSFU/SUA subsystem which provides a native implementation of a number of POSIX APIs, and also withinthird-party packages such aspthreads-w32,[2] which implements pthreads on top of existingWindows API.
pthreads defines a set ofC programming languagetypes,functions and constants. It is implemented with apthread.h header and a threadlibrary.
There are around 100 threads procedures, all prefixedpthread_ and they can be categorized into five groups:
The POSIXsemaphore API works with POSIX threads but is not part of the threads standard, having been defined in thePOSIX.1b, Real-time extensions (IEEE Std 1003.1b-1993) standard. Consequently, the semaphore procedures are prefixed bysem_ instead ofpthread_.
An example illustrating the use of pthreads in C:
#include<assert.h>#include<stdio.h>#include<stdlib.h>#include<pthread.h>#include<unistd.h>#define NUM_THREADS 5void*perform_work(void*arguments){intindex=*((int*)arguments);intsleep_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);returnNULL;}intmain(void){pthread_tthreads[NUM_THREADS];intthread_args[NUM_THREADS];intresult_code;//create all threads one by onefor(inti=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 completefor(inti=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");return0;}
This program creates five threads, each executing the functionperform_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 aglobal variable. This program can be compiled using thegcc 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.
Windows does not support the pthreads standard natively; therefore, the Pthreads4w project seeks to provide a portable and open-sourcewrapper implementation. It can also be used to portUnix software (which uses pthreads) with little or no modification to the Windows platform.[4] Pthreads4w version 3.0.0[5] or later, released under the Apache Public License v2.0, is compatible with 64-bit or 32-bit Windows systems. Version 2.11.0,[6] released under the LGPLv3 license, is also 64-bit or 32-bit compatible.
TheMingw-w64 project also contains a wrapper implementation of 'pthreads,winpthreads, which tries to use more native system calls than the Pthreads4w project.[7]
Interix environment subsystem available in theWindows 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 systemsyscall interface.[8]