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 five groups:
The POSIX semaphore 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_.
An example illustrating the use of pthreads in C:
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:
Here is one of the many possible outputs from running this program.
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.4 Pthreads4w version 3.0.05 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.
The Mingw-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 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.8
"libc/bionic/pthread.c - platform/bionic - Git at Google". android.googlesource.com. https://android.googlesource.com/platform/bionic/+/10ce969/libc/bionic/pthread.c ↩
"Pthread Win-32: Level of standards conformance". 2006-12-22. Archived from the original on 2010-06-11. Retrieved 2010-08-29. https://web.archive.org/web/20100611172430/http://sources.redhat.com/pthreads-win32/conformance.html ↩
"pthread.h(0p) — Linux manual page". Retrieved 18 December 2022. https://www.man7.org/linux/man-pages/man0/pthread.h.0p.html ↩
Hart, Johnson M. (2004-11-21). "Experiments with the Open Source Pthreads Library and Some Comments". Archived from the original on 2010-08-30. Retrieved 2010-08-29. https://web.archive.org/web/20100830235615/http://world.std.com/~jmhart/opensource.htm ↩
File: pthreads4w-code-v3.0.0.zip – Source for pthreads4w v3.0.0 https://sourceforge.net/projects/pthreads4w/files/ ↩
File: pthreads4w-code-v2.11.0.zip – Source for pthreads4w v2.11.0 https://sourceforge.net/projects/pthreads4w/files/ ↩
see http://locklessinc.com/articles/pthreads_on_windows which is where it was originally derived from http://locklessinc.com/articles/pthreads_on_windows ↩
"Chapter 1: Introduction to Windows Services for UNIX 3.5". 5 December 2007. https://technet.microsoft.com/en-us/library/bb496994.aspx ↩