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, Mac OS X, Android[1], Solaris 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


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


The mingw-w64 project also contains a wrapper implementation of pthreads, winpthreads,[8] which tries to use more native system calls than the Pthreads-w32 project.[9]


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



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". android.googlesource.com..mw-parser-output cite.citation{font-style:inherit}.mw-parser-output q{quotes:"""""""'""'"}.mw-parser-output code.cs1-code{color:inherit;background:inherit;border:inherit;padding:inherit}.mw-parser-output .cs1-lock-free a{background:url("//upload.wikimedia.org/wikipedia/commons/thumb/6/65/Lock-green.svg/9px-Lock-green.svg.png")no-repeat;background-position:right .1em center}.mw-parser-output .cs1-lock-limited a,.mw-parser-output .cs1-lock-registration a{background:url("//upload.wikimedia.org/wikipedia/commons/thumb/d/d6/Lock-gray-alt-2.svg/9px-Lock-gray-alt-2.svg.png")no-repeat;background-position:right .1em center}.mw-parser-output .cs1-lock-subscription a{background:url("//upload.wikimedia.org/wikipedia/commons/thumb/a/aa/Lock-red-alt-2.svg/9px-Lock-red-alt-2.svg.png")no-repeat;background-position:right .1em center}.mw-parser-output .cs1-subscription,.mw-parser-output .cs1-registration{color:#555}.mw-parser-output .cs1-subscription span,.mw-parser-output .cs1-registration span{border-bottom:1px dotted;cursor:help}.mw-parser-output .cs1-hidden-error{display:none;font-size:100%}.mw-parser-output .cs1-visible-error{font-size:100%}.mw-parser-output .cs1-subscription,.mw-parser-output .cs1-registration,.mw-parser-output .cs1-format{font-size:95%}.mw-parser-output .cs1-kern-left,.mw-parser-output .cs1-kern-wl-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right,.mw-parser-output .cs1-kern-wl-right{padding-right:0.2em}


  2. ^ "Pthread Win-32: Level of standards conformance". 2006-12-22. Retrieved 2010-08-29.


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


  4. ^ "pthread-win32_x64.zip Source and binary for pthreads-w32 v2.8.0". 2010-01-26. Retrieved 2010-08-29.


  5. ^ "Forum discussion: pthreads-on-64bit-Windows". 2010-01-26. Archived from the original on 2010-12-15. Retrieved 2010-08-29.


  6. ^ "Compile pthreads – mingw-w64". sourceforge.net. Archived from the original on 2012-07-02. Retrieved 2012-07-26.


  7. ^ http://sourceware.org/pthreads-win32/news.html -- the "64 bit" mentions


  8. ^ mingw-w64 - Revision 5520: /experimental/winpthreads[permanent dead link]


  9. ^ see http://locklessinc.com/articles/pthreads_on_windows which is where it was originally derived from


  10. ^ "Chapter 1: Introduction to Windows Services for UNIX 3.5".




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.


  • Charles J. Northrup (1996-01-25). Programming with UNIX Threads. John Wiley & Sons. ISBN 978-0-471-13751-1.


  • Kay A. Robbins & Steven Robbins (2003). UNIX Systems Programming. Prentice-Hall. ISBN 978-0-13-042411-2.



External links


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








Popular posts from this blog

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values