POSIX THREADS

(Redirected from POSIX threads)
'POSIX Threads' is a POSIX standard for threads. The standard defines an API for creating and manipulating threads.
Libraries implementing the POSIX Threads standard are often named 'Pthreads'. Pthreads are most commonly used on Unix-like POSIX systems such as Linux and Solaris, but Microsoft Windows implementations also exist. For example, the pthreads-w32 is available and supports a subset of the Pthread API [1]. (Note: in text, Pthreads is written with an upper-case P.)

Contents
Contents
Example
References
See also
External links

Contents


Pthreads defines a set of C programming language types and procedure calls. It is implemented with a pthread.h header and a thread library.
Data types

★ pthread_t: handle to a thread

★ pthread_attr_t: thread attributes
Thread manipulation functions: arguments omitted for brevity.

★ pthread_create(): create a thread

★ pthread_exit(): terminate current thread

★ pthread_cancel(): cancel another thread

★ pthread_join(): block current thread until another one terminiates

★ pthread_attr_init(): initialize thread attributes

★ pthread_attr_setdetachstate():

★ pthread_attr_destroy(): destroy thread attributes
Synchronization functions: for mutexes and condition variables

★ pthread_mutex_init ()

★ pthread_mutex_destroy ()

★ pthread_mutex_lock ()

★ pthread_mutex_trylock ( )

★ pthread_mutex_unlock ()

★ pthread_cond_init()

★ pthread_cond_signal

★ pthread_cond_wait()

Example


An example of using Pthreads in C:

#include
#include
#include
void
★ thread_func( void
★ vptr_args );
int main( void ){
int i, j;
pthread_t thread;
pthread_create( &thread, NULL, &thread_func, NULL );
for( j= 0; j < 20; ++j ){
fprintf( stdout, "a
" );
for( i= 99999999; i; --i ); /
★ use some CPU time
★ /
}
pthread_join( thread, NULL );
exit( EXIT_SUCCESS );
}
void
★ thread_func( void
★ vptr_args ){
int i, j;
for( j= 0; j < 20; ++j ){
fprintf( stderr, " b
" );
for( i= 99999999; i; --i ); /
★ use some CPU time
★ /
}
pthread_exit( NULL );
}

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. More tutorials can be found below in the links section.

References



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

See also



Native POSIX Thread Library (NPTL)

Spurious wakeup

GNU Portable Threads

External links



POSIX Threading and Synchronous Wrappers

Multithreaded Programming (Pthreads Tutorial)

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

This article provided by Wikipedia. To edit the contents of this article, click here for original source.

psst.. try this: add to faves