Вы находитесь на странице: 1из 4

11/28/12

POSIX Threads - Wikipedia, the f ree ency clopedia

POSIX Threads
From Wikipedia, the free encyclopedia

POSIX Threads , usually referred to as Pthreads , 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, 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
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 p t h r e a d . h( h t t p : / / o p e n g r o u p . o r g / o n l i n e p u b s / 0 0 7 9 0 8 7 9 9 / x s h / p t h r e a d . h . h t m l ) 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 POSIX.1b, Real-time extensions (IEEE Std 1003.1b-1993) standard. Consequently the semaphore procedures are prefixed by "sem_" instead of "pthread_".

Example
en.wikipedia.org/wiki/POSIX_Threads 1/4

11/28/12

POSIX Threads - Wikipedia, the f ree ency clopedia

An example illustrating the use of Pthreads in C:


# i n c l u d e< p t h r e a d . h > # i n c l u d e< s t d i o . h > # i n c l u d e< s t d l i b . h > # i n c l u d e< a s s e r t . h > # d e f i n eN U M _ T H R E A D S 5

v o i d* T a s k C o d e ( v o i d* a r g u m e n t ) { i n tt i d ; t i d=* ( ( i n t* )a r g u m e n t ) ; p r i n t f ( " H e l l oW o r l d !I t ' sm e ,t h r e a d% d ! \ n " ,t i d ) ; / *o p t i o n a l l y :i n s e r tm o r eu s e f u ls t u f fh e r e* / } r e t u r nN U L L ;

i n tm a i n ( v o i d ) { p t h r e a d _ tt h r e a d s [ N U M _ T H R E A D S ] ; i n tt h r e a d _ a r g s [ N U M _ T H R E A D S ] ; i n tr c ,i ; / *c r e a t ea l lt h r e a d s* / f o r( i = 0 ;i < N U M _ T H R E A D S ;+ + i ){ t h r e a d _ a r g s [ i ]=i ; p r i n t f ( " I nm a i n :c r e a t i n gt h r e a d% d \ n " ,i ) ; r c=p t h r e a d _ c r e a t e ( & t h r e a d s [ i ] ,N U L L ,T a s k C o d e ,( v o i d* )& t h r e a d _ a r g s [ i ] ) ; a s s e r t ( 0= =r c ) ; } / *w a i tf o ra l lt h r e a d st oc o m p l e t e* / f o r( i = 0 ;i < N U M _ T H R E A D S ;+ + i ){ r c=p t h r e a d _ j o i n ( t h r e a d s [ i ] ,N U L L ) ; a s s e r t ( 0= =r c ) ; } } e x i t ( E X I T _ S U C C E S S ) ;

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 (http://sourceware.org/pthreads-win32/) 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
en.wikipedia.org/wiki/POSIX_Threads 2/4

OpenMP 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)

References
1. ^ "Pthread Win-32: Level of standards conformance" (http://sources.redhat.com/pthreadswin32/conformance.html) . 2006-12-22. http://sources.redhat.com/pthreads-win32/conformance.html. Retrieved 2010-08-29. 2. ^ Hart, Johnson M. (2004-11-21). "Experiments with the Open Source Pthreads Library and Some Comments" (http://world.std.com/~jmhart/opensource.htm) . http://world.std.com/~jmhart/opensource.htm. Retrieved 2010-08-29. 3. ^ "pthread-win32_x64.zip Source and binary for Pthreads-w32 v2.8.0" (https://code.google.com/p/libusbwinusb-wip/downloads/detail?name=pthread-win32_x64.zip&can=2&q=) . 2010-01-26. https://code.google.com/p/libusb-winusb-wip/downloads/detail?name=pthread-win32_x64.zip&can=2&q=. Retrieved 2010-08-29. 4. ^ "Forum discussion: pthreads-on-64bit-Windows" (http://libusb.6.n5.nabble.com/pthreads-on-64bit-Windowstd48i20.html) . 2010-01-26. http://libusb.6.n5.nabble.com/pthreads-on-64bit-Windows-td48i20.html. 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" (http://technet.microsoft.com/enus/library/bb496994.aspx) . http://technet.microsoft.com/en-us/library/bb496994.aspx. 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. 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-13042411-0.

External links
Pthreads Tutorial (https://computing.llnl.gov/tutorials/pthreads/) C/C++ Tutorial: using Pthreads (http://yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html) Article "POSIX threads explained (http://www-128.ibm.com/developerworks/linux/library/l-posix1.html) " by Daniel Robbins (Gentoo Linux founder) Interview "Ten Questions with David Butenhof about Parallel Programming and POSIX Threads
en.wikipedia.org/wiki/POSIX_Threads

(http://www.thinkingparallel.com/2007/04/11/ten-questions-with-david-butenhof-about-parallel-

3/4

11/28/12

POSIX Threads - Wikipedia, the f ree ency clopedia

(http://www.thinkingparallel.com/2007/04/11/ten-questions-with-david-butenhof-about-parallelprogramming-and-posix-threads/) " by Michael Suess Open Source POSIX Threads for Win32 (http://sources.redhat.com/pthreads-win32/) The Open Group Base Specifications Issue 6, IEEE Std 1003.1 (http://www.opengroup.org/onlinepubs/007904975/basedefs/pthread.h.html) Pthreads Presentation at 2007 OSCON (O'Reilly Open Source Convention) by Adrien Lamothe. An overview of Pthreads with current trends. (http://conferences.oreillynet.com/presentations/os2007/os_lamothe.pdf) Comparing programmability of Open MP and pthreads (http://www.futurechips.org/tips-for-powercoders/open-mp-pthreads.html) Retrieved from "http://en.wikipedia.org/w/index.php?title=POSIX_Threads&oldid=519372429" Categories: C POSIX library Parallel computing Threads (computing) This page was last modified on 23 October 2012 at 11:42. Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. See Terms of Use for details. Wikipedia is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.

en.wikipedia.org/wiki/POSIX_Threads

4/4

Вам также может понравиться