World Library  
Flag as Inappropriate
Email this Article

POSIX Threads

Article Id: WHEBN0000634671
Reproduction Date:

Title: POSIX Threads  
Author: World Heritage Encyclopedia
Language: English
Subject: Parallel computing, List of sequence alignment software, Memory barrier, NEST (software), POSIX
Collection: C Posix Library, Parallel Computing, Threads (Computing)
Publisher: World Heritage Encyclopedia

POSIX Threads

POSIX Threads, usually referred to as Pthreads, is an example of an execution model that exists independently from a language, as well as an example of 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 a POSIX standard for threads. The standard, POSIX.1c, Threads extensions (IEEE Std 1003.1c-1995), defines an API for creating and manipulating threads.

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. 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.


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


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 Pthreads procedures, all prefixed "pthread_" and they can be categorized into four groups:

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_".


An example illustrating the use of Pthreads in C:

#define NUM_THREADS     5
void *perform_work(void *argument)
   int passed_in_value;
   passed_in_value = *((int *) argument);
   printf("Hello World! It's me, thread with argument %d!\n", passed_in_value);
   /* optionally: insert more useful stuff here */
   return NULL;
int main(void)
   pthread_t threads[NUM_THREADS];
   int thread_args[NUM_THREADS];
   int result_code, index;
   // create all threads one by one
   for (index = 0; index < NUM_THREADS; ++index) {
      thread_args[index] = index;
      printf("In main: creating thread %d\n", index);
      result_code = pthread_create(&threads[index], NULL, perform_work, (void *) &thread_args[index]);
      assert(0 == result_code);
   // wait for each thread to complete
   for (index = 0; index < NUM_THREADS; ++index) {
      // block until thread 'index' completes
      result_code = pthread_join(threads[index], NULL);
      printf("In main: thread %d has completed\n", index);
      assert(0 == result_code);
   printf("In main: All threads completed successfully\n");

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

POSIX Threads for Windows

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 platform.[2] With some additional patches the last version 2.8.0 is compatible with 64-bit Windows systems.[3][4][5] 2.9.0 is said to also be 64-bit compatible.[6]

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 project.[8]

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.[9]

See also


  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. ^ " Source and binary for Pthreads-w32 v2.8.0". 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. ^ -- the "64 bit" mentions
  7. ^ mingw-w64 - Revision 5520: /experimental/winpthreads
  8. ^ see which is where it was originally derived from
  9. ^ "Chapter 1: Introduction to Windows Services for UNIX 3.5". 

Further reading

  • David R. Butenhof. Programming with POSIX Threads. Addison-Wesley.  
  • Bradford Nichols, Dick Buttlar, Jacqueline Proulx Farell. Pthreads Programming. O'Reilly & Associates.  
  • Charles J. Northrup. Programming with UNIX Threads. John Wiley & Sons.  
  • Kay A. Robbins and Steven Robbins. UNIX Systems Programming. Prentice-Hall.  

External links

  • The Open Group Base Specifications Issue 7, IEEE Std 1003.1
  • HTML5 Presentation on Multi-Threading & POSIX Thread APIs
This article was sourced from Creative Commons Attribution-ShareAlike License; additional terms may apply. World Heritage Encyclopedia content is assembled from numerous content providers, Open Access Publishing, and in compliance with The Fair Access to Science and Technology Research Act (FASTR), Wikimedia Foundation, Inc., Public Library of Science, The Encyclopedia of Life, Open Book Publishers (OBP), PubMed, U.S. National Library of Medicine, National Center for Biotechnology Information, U.S. National Library of Medicine, National Institutes of Health (NIH), U.S. Department of Health & Human Services, and, which sources content from all federal, state, local, tribal, and territorial government publication portals (.gov, .mil, .edu). Funding for and content contributors is made possible from the U.S. Congress, E-Government Act of 2002.
Crowd sourced content that is contributed to World Heritage Encyclopedia is peer reviewed and edited by our editorial staff to ensure quality scholarly research articles.
By using this site, you agree to the Terms of Use and Privacy Policy. World Heritage Encyclopedia™ is a registered trademark of the World Public Library Association, a non-profit organization.

Copyright © World Library Foundation. All rights reserved. eBooks from Project Gutenberg are sponsored by the World Library Foundation,
a 501c(4) Member's Support Non-Profit Organization, and is NOT affiliated with any governmental agency or department.