POSIX Threads

From Wikipedia, the free encyclopedia

(Redirected from Pthreads)
Jump to: navigation, search

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.[citation needed])

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 execution of another thread
  • pthread_join(): block current thread until another one terminates
  • pthread_attr_init(): initialize thread attributes
  • pthread_attr_setdetachstate(): set the detachstate attribute (whether thread can be joined on termination)
  • pthread_attr_getdetachstate(): get the detachstate attribute
  • pthread_attr_destroy(): destroy thread attributes
  • pthread_kill(): send a signal to a thread

Synchronization functions: for mutexes and condition variables

  • pthread_mutex_init()
  • pthread_mutex_destroy()
  • pthread_mutex_lock(): acquire mutex lock (blocking)
  • pthread_mutex_trylock(): acquire mutex lock (non-blocking)
  • pthread_mutex_unlock(): release mutex lock
  • pthread_cond_init()
  • pthread_cond_destroy()
  • pthread_cond_signal(): signal a condition
  • pthread_cond_wait(): wait on a condition

An example of using Pthreads in C:

#include 
#include 
#include 
#include 
 
void *thread_func( void *vptr_args );
 
void do_some_work( void ){
  time_t start_time = time(NULL);
 
  while (time(NULL) == start_time);  /* busy-wait for 0-1 seconds */
}
 
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\n" );
 
    do_some_work();
  }
 
  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\n" );
 
    do_some_work();
  }
  return 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.

Advanced Search
Included Web Search Engines


Safe Search

close

Top Matching Results

Occasionally Search.com will highlight specialized results that are based on the context of your query. Examples of specialized results include specific links to news, images, or video.

Top Matching Results may highlight information from other Search.com pages, content from the CNET Network of sites, or third party content. The listings are based purely on relevance. Search.com does not receive payment for listings in this section but our partners that provide this data may get paid for listing these products.

Sponsored Links

This section contains paid listings which have been purchased by companies that want to have their sites appear for specific search terms and related content. These listings are administered, sorted and maintained by a third party and are not endorsed by Search.com.

Search Results

Search.com sends your search query to several search engines at one time and integrates the results into one list which has been sorted by relevance using Search.com's proprietary algorithm. You can customize the list of search engines included in your metasearch from the preferences.

The search engines that are used in your metasearch may allow companies to pay to have their Web sites included within the results. To view the Paid Inclusion policy for a specific search engine, please visit their Web site. Search.com does not accept payment or share revenue with any search engine partner for listings in this section.