Jump to content

Pthreads: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Revert: test edits? by 117.202.163.78 (talk) to last revision by Rogerdpack
RzR~enwiki (talk | contribs)
No edit summary
Line 2: Line 2:
{{Citation style|date=April 2010}}
{{Citation style|date=April 2010}}


'''POSIX Threads''', usually referred to as '''Pthreads''', is a [[POSIX]] standard for [[thread (computer science)|thread]]s. The standard, ''POSIX.1c, Threads extensions (IEEE Std 1003.1c-1995)'', defines an [[Application programming interface|API]] for creating and manipulating threads.
'''POSIX Threads''', usually referred to as '''Pthreads''', is a [[POSIX]] standard for [[thread ([[computer science]])|thread]]s. The standard, ''POSIX.1c, Threads extensions ([[IEEE]] Std 1003.1c-1995)'', defines an [[Application programming interface|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|GNU/Linux]], [[Mac OS X]] and [[Solaris (operating system)|Solaris]]. [[DR-DOS]] and [[Microsoft Windows]] implementations also exist: within the [[Windows Services for UNIX|SFU/SUA]] subsystem which provides a [[native implementation]] of a number of POSIX APIs, and also within [[third-party]] packages such as ''pthreads-w32'',<ref>{{cite web|url=http://sources.redhat.com/pthreads-win32/conformance.html| title=Pthread Win-32: Level of standards conformance|date=2006-12-22|accessdate=2010-08-29}}</ref> which implements pthreads on top of existing [[Windows API]].


Implementations of the API are available on many [[Unix-like]] POSIX-conformant operating systems such as [[FreeBSD]], [[NetBSD]], [[OpenBSD]], [[Linux|GNU/Linux]], [[Mac OS X]] and [[Solaris (operating system)|Solaris]]. [[DR-DOS]] and [[Microsoft Windows]] implementations also exist: within the [[Windows Services for UNIX|SFU/SUA]] subsystem which provides a native implementation of a number of POSIX APIs, and also within third-party packages such as ''pthreads-w32'',<ref>{{cite web|url=http://sources.redhat.com/pthreads-win32/conformance.html| title=Pthread Win-32: Level of standards conformance|date=2006-12-22|accessdate=2010-08-29}}</ref> which implements pthreads on top of existing [[Windows API]].


==Contents==
==Contents==

Pthreads defines a set of [[C (programming language)|C]] programming language [[data type|types]], [[function (computer science)|functions]] and constants. It is implemented with a <tt>[http://opengroup.org/onlinepubs/007908799/xsh/pthread.h.html pthread.h]</tt> header and a thread library.
Pthreads defines a set of [[C (programming language)|C]] programming language [[data type|types]], [[function (computer science)|functions]] and constants. It is implemented with a <tt>[http://opengroup.org/onlinepubs/007908799/xsh/pthread.h.html pthread.h]</tt> header and a thread [[library]].


There are around 100 Pthreads procedures, all prefixed "pthread_" and they can be categorized into four groups:
There are around 100 Pthreads procedures, all prefixed "pthread_" and they can be categorized into four groups:
Line 17: Line 19:


The POSIX [[semaphore (programming)|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_".
The POSIX [[semaphore (programming)|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==
==Example==

An example illustrating the use of Pthreads in C:
An example illustrating the use of Pthreads in C:



Revision as of 14:03, 22 October 2012

‹The template Manual is being considered for merging.› 

POSIX Threads, usually referred to as Pthreads, is a POSIX standard for [[thread (computer science)|thread]]s. 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, GNU/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

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


Example

An example illustrating the use of Pthreads in C:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
 
#define NUM_THREADS     5
 
void *TaskCode(void *argument)
{
   int tid;
 
   tid = *((int *) argument);
   printf("Hello World! It's me, thread %d!\n", tid);
 
   /* optionally: insert more useful stuff here */
 
   return NULL;
}
 
int main(void)
{
   pthread_t threads[NUM_THREADS];
   int thread_args[NUM_THREADS];
   int rc, i;
 
   /* create all threads */
   for (i=0; i<NUM_THREADS; ++i) {
      thread_args[i] = i;
      printf("In main: creating thread %d\n", i);
      rc = pthread_create(&threads[i], NULL, TaskCode, (void *) &thread_args[i]);
      assert(0 == rc);
   }
 
   /* wait for all threads to complete */
   for (i=0; i<NUM_THREADS; ++i) {
      rc = pthread_join(threads[i], NULL);
      assert(0 == rc);
   }
 
   exit(EXIT_SUCCESS);
}

This program creates five threads, each executing the function TaskCode 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.

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 be 64-bit compatible.[6]

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

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

See also

References

  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_x64.zip 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. ^ https://sourceforge.net/apps/trac/mingw-w64/wiki/Compile%20pthreads
  6. ^ http://sourceware.org/pthreads-win32/news.html -- the "64 bit" mentions
  7. ^ "Chapter 1: Introduction to Windows Services for UNIX 3.5".
  8. ^ https://mingw-w64.svn.sourceforge.net/svnroot/mingw-w64/experimental/winpthreads
  9. ^ see http://locklessinc.com/articles/pthreads_on_windows which is where it was originally derived from

Further reading

  • 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.{{cite book}}: CS1 maint: multiple names: authors list (link)
  • Charles J. Northrup. Programming with UNIX Threads. John Wiley & Sons. ISBN 0-471-13751-0.
  • Kay A. Robbins and Steven Robbins. UNIX Systems Programming. Prentice-Hall. ISBN 0-13-042411-0.