Posix Threads Shopping
Posix
Website Links For
Posix
 

Information About

Posix 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 {Link without Title} . (Note: in text, Pthreads is written with an upper-case P.)


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

  • 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
" );

  • use some CPU time ---/

  • }


pthread_join( thread, NULL );

exit( EXIT_SUCCESS );
}

  • thread_func( void ---vptr_args ){

  • int i, j;


for( j= 0; j < 20; ++j ){
fprintf( stderr, " b
" );

  • 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



SEE ALSO



EXTERNAL LINKS