Ah, a nodeshell to claim as my own!

POSIX thread, for multithreaded programming on a UNIX-like system. Here's an incredibly basic example of how to use pthreads (you should really look into manpages...)
  When compiling, link this to libpthread:
  $ cc filename.c -o filename -lpthread

#include <stdio.h>
#include <unistd.h>

#include <pthread.h>

void *my_first_thread( void *arg ) {

  printf( "This is my first thread; looping infinitely\n" );


void *my_second_thread( void *arg ) {

  printf( "This is my second thread\n" );
  sleep( 2 );
  return NULL;


int main() {

  pthread_t my_thread1, my_thread2;
  int r;
  void *return_value;

  r = pthread_create( &my_thread1, NULL, *my_first_thread, NULL );

  if( r != 0 ) {
    fprintf( stderr, "failed to create thread\n" );
    return 1;

  r = pthread_create( &my_thread2, NULL, *my_second_thread, NULL );

  if( r != 0 ) {
    fprintf( stderr, "failed to create thread\n" );
    return 1;

  pthread_join( my_thread2, &return_value ); 
  printf( "my_second_thread() ended returning %p, so now I shall quit.\n",
          return_value );
  return 0;

A good starting point for learning Pthread programming is the book:

"Programming with POSIX Threads", by David R. Butenhof.

I especially like this book because it discusses a lot of the practical issues you run into with multithreaded programming. One of the points Butemhof makes is to never simply assume that a thread will arrive a certain point in the code before something else happens in another thread. This is one of the more non-intuitive concepts in writing parallel code.

Pthreads is a standard interface for creating and managing threads on Unix operating systems. In particular, it is a set of C language types and functions that have a well-defined behavior.

The P in Pthreads stands for "POSIX", the set of standards that most Unix operating systems adhere to. Pthreads are defined in IEEE POSIX 1003.1c-1995. A popular superset of the standard is defined in CAE Specification System Interfaces & Headers Issue 5 (XSH5) of the Single UNIX Specification, Version 2.

The library itself provides three major areas of functionality:

  • Creating, managing, running, and deleting threads:
    pthread_create, pthread_exit, pthread_join, ...
  • Synchronizing threads with mutexes and condition variables:
    pthread_mutex_lock, pthread_mutex_unlock, pthread_cond_wait, ...
  • Managing thread specific data:
    pthread_setspecific, pthread_getspecific, ...

Log in or register to write something here or to contact authors.