Tue . 19 Sep 2019
TR | RU | UK | KK | BE |

POSIX Threads

posix threads, posix threads tutorial
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 POSIX1c, Threads extensions IEEE Std 10031c-1995

Implementations of the API are available on many Unix-like POSIX-conformant operating systems such as FreeBSD, NetBSD, OpenBSD, Linux, Mac OS X and Solaris, 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,1 which implements pthreads on top of existing Windows API


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


Pthreads defines a set of C programming language types, functions and constants It is implemented with a pthreadh header and a thread library

There are around 100 Pthreads 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 POSIX1b, Real-time extensions IEEE Std 10031b-1993 standard Consequently, the semaphore procedures are prefixed by sem_ instead of pthread_


An example illustrating the use of Pthreads in C:

#include <pthreadh> #include <stdioh> #include <stdlibh> #include <asserth> #define NUM_THREADS 5 void perform_work void argument int main int argc, char argv // wait for each thread to complete for index = 0; index < NUM_THREADS; ++index printf "In main: All threads completed successfully\n" ; exit EXIT_SUCCESS ;

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_democ -pthread -o pthreads_demo

POSIX Threads for Windowsedit

Windows does not support the pthreads standard natively, therefore the Pthreads-w32 project seeks to provide a portable and open-source wrapper implementation It can also be used to port Unix software which use pthreads with little or no modification to the Windows platform2 With some additional patches the last version 280 is compatible with 64-bit Windows systems345 290 is said to also be 64-bit compatible6

The mingw-w64 project also contains a wrapper implementation of pthreads, winpthreads,7 which tries to use more native system calls than the Pthreads-w32 project8

Interix environment subsystem available in the Windows Services for UNIX/Subsystem for UNIX-based Applications package provides a native port of the pthreads API, ie not mapped on Win32/Win64 API but built directly on the operating system syscall interface9

See alsoedit

  • Execution model
  • 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


  1. ^ "Pthread Win-32: Level of standards conformance" 2006-12-22 Retrieved 2010-08-29 
  2. ^ Hart, Johnson M 2004-11-21 "Experiments with the Open Source Pthreads Library and Some Comments" Retrieved 2010-08-29 
  3. ^ "pthread-win32_x64zip Source and binary for Pthreads-w32 v280" 2010-01-26 Retrieved 2010-08-29 
  4. ^ "Forum discussion: pthreads-on-64bit-Windows" 2010-01-26 Retrieved 2010-08-29 
  5. ^ Compile pthreads – mingw-w64
  6. ^ http://sourcewareorg/pthreads-win32/newshtml -- the "64 bit" mentions
  7. ^ mingw-w64 - Revision 5520: /experimental/winpthreads
  8. ^ see http://locklessinccom/articles/pthreads_on_windows which is where it was originally derived from
  9. ^ "Chapter 1: Introduction to Windows Services for UNIX 35" 

Further readingedit

  • David R Butenhof Programming with POSIX Threads Addison-Wesley ISBN 0-201-63392-2 
  • Bradford Nichols; Dick Buttlar; Jacqueline Proulx Farell Pthreads Programming O'Reilly & Associates ISBN 1-56592-115-1 
  • Charles J Northrup Programming with UNIX Threads John Wiley & Sons ISBN 0-471-13751-0 
  • Kay A Robbins & Steven Robbins UNIX Systems Programming Prentice-Hall ISBN 0-13-042411-0 

External linksedit

  • The Open Group Base Specifications Issue 7, IEEE Std 10031

posix threads, posix threads c, posix threads c++ linux, posix threads for win32, posix threads for windows, posix threads linux, posix threads programming, posix threads programming pdf, posix threads tutorial, posix threads windows

POSIX Threads Information about

POSIX Threads

  • user icon

    POSIX Threads beatiful post thanks!


POSIX Threads
POSIX Threads
POSIX Threads viewing the topic.
POSIX Threads what, POSIX Threads who, POSIX Threads explanation

There are excerpts from wikipedia on this article and video

Random Posts



A book is a set of written, printed, illustrated, or blank sheets, made of ink, paper, parchment, or...
Boston Renegades

Boston Renegades

Boston Renegades was an American women’s soccer team, founded in 2003 The team was a member of the U...
Sa Caleta Phoenician Settlement

Sa Caleta Phoenician Settlement

Sa Caleta Phoenician Settlement can be found on a rocky headland about 10 kilometers west of Ibiza T...


Bodybuildingcom is an American online retailer based in Boise, Idaho, specializing in dietary supple...