POSIX Threads
From Wikipedia, the free encyclopedia
This article's citation style may be unclear. The references used may be clearer with a different or consistent style of citation, footnoting, or external linking. |
POSIX Threads, or Pthreads, is a POSIX standard for threads. The standard defines an API for creating and manipulating threads. Pthreads are most commonly used on Unix-like POSIX systems such as Linux, Mac OS X and Solaris, but Microsoft Windows implementations also exist. For example, the pthreads-w32 is available and supports a subset of the Pthread API for the Windows 32-bit platform.[1]
Contents |
[edit] 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. Programmers can use Pthreads to create, manipulate and manage threads, as well as synchronize between threads using mutexes and signals.
[edit] Example
An example of using Pthreads in C:
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <pthread.h> static void wait_thread(void) { time_t start_time = time(NULL); while (time(NULL) == start_time) { // do nothing except chew CPU slices for up to one second. } } static void *thread_func(void *vptr_args) { int i; for (i = 0; i < 20; i++) { fputs(" b\n", stderr); wait_thread(); } return NULL; } int main(void) { int i; pthread_t thread; if (pthread_create(&thread, NULL, thread_func, NULL) != 0) { return EXIT_FAILURE; } for (i = 0; i < 20; i++) { fputs("a\n", stdout); wait_thread(); } if (pthread_join(thread, NULL) != 0) { return EXIT_FAILURE; } return EXIT_SUCCESS; }
An example of using Pthreads in C++:
#include <stdio.h> #include <time.h> #include <unistd.h> #include <pthread.h> class Thread{ private: pthread_t thread; static void *thread_func(void *d){((Thread *)d)->run();} public: Thread(){} virtual ~Thread(){} virtual void run(){} int start(){return pthread_create(&thread, NULL, Thread::thread_func, (void*)this);} int wait(){return pthread_join(thread, NULL);} }; int main(void){ class Thread_a:public Thread{ public: void run(){for(int i=0;i<20;i++){sleep(1);printf("a \n");}} }; class Thread_b:public Thread{ public: void run(){for(int i=0;i<20;i++){sleep(1);printf(" b\n");}} }; Thread *a=new Thread_a(); Thread *b=new Thread_b(); if (a->start() != 0) { return EXIT_FAILURE; } if (b->start() != 0) { return EXIT_FAILURE; } if (a->wait() != 0) { return EXIT_FAILURE; } if (b->wait() != 0) { return EXIT_FAILURE; } delete a; delete b; return EXIT_SUCCESS; }
This program creates a new thread that prints lines containing 'b', while the main thread prints lines containing 'a'. The output is interleaved between 'a' and 'b' as a result of execution switching between the two threads, or simultaneous execution on a multicore system. In any case, the pattern of 'a's and 'b's does not strictly alternate between each letter, and can vary even between different runs on the same machine.
[edit] References
[edit] 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-13-042411-0
[edit] See also
- OpenMP
- Native POSIX Thread Library (NPTL)
- Spurious wakeup
- Thread-local storage
- GNU Portable Threads
- FSU Pthreads
[edit] External links
- Pthread Win-32, Basic Programming
- Pthreads Tutorial
- C/C++ Tutorial: using Pthreads
- Article "POSIX threads explained" by Daniel Robbins (Gentoo Linux founder)
- Interview "Ten Questions with David Butenhof about Parallel Programming and POSIX Threads" by Michael Suess
- Open Source POSIX Threads for Win32
- The Open Group Base Specifications Issue 6, IEEE Std 1003.1
- GNU Portable threads
- Flash Presentation on pThread
- Pthreads Presentation at 2007 OSCON (O'Reilly Open Source Convention) by Adrien Lamothe. An overview of Pthreads with current trends.
|