Introduction to Posix Thread Programming API

Source: Internet
Author: User

Original article: http://baike.baidu.com/view/974776.htm

Http://zh.wikipedia.org/wiki/POSIXhttp://www.blogjava.net/tinysun/archive/2010/05/29/322210.html I. IntroductionPOSIX thread is called Posix thread (pthread for short) and Posix thread is a POSIX standard thread. POSIX is a general term for IEEE to define a series of associated APIs for software running on Various UNIX operating systems. It is officially called IEEE 1003, the international standard is named ISO/IEC 9945. This standard originated from a project that started around 1985. POSIX is an easy-to-remember name proposed by Richard stoman at IEEE's request. It is basically the abbreviation of Portable Operating System Interface (the Interface that can be transplanted to the Operating System), and X indicates its inheritance of Unix APIs. Linux has basically achieved POSIX compatibility, but does not participate in formal POSIX authentication. Microsoft's Windows NT claims to partially implement the POSIX standard .. Pthreads defines a set of C program language types, functions, and constants. It uses the pthread. h header file and a thread Library

Ii. Data Types

Pthread_t: thread handle pthread_attr_t: thread attribute pthread _ T Type Definition:

  typedef unsigned long int pthread_t;  //come from /usr/include/bits/pthread.h

Purpose: Use pthread_t to declare the thread ID. Others: sizeof (pthread_t) = 4; Iii. Thread-manipulation functionsPthread_create (): Creates a thread. for more information, see pthread_create introduction pthread_exit (): Terminate the current thread. for more information, see pthread_exit introduction pthread_cancel (): interrupt the running of another thread. example 1:

pthread_t thr; void *res; pthread_create(&thr, NULL, thread_start, NULL); sleep(2); /* Allow new thread to run a while */ pthread_cancel(thr) pthread_join(thr, &res); if(res == PTHREAD_CANCELED) printf("Thread was canceled; cnt = %d\n", cnt); else printf("Thread terminated normally; cnt = %d\n", cnt); exit(EXIT_SUCCESS);;

Int pthread_join (pthread_t thread, void ** retval);: blocks the current thread until another thread stops running. See example 1. For more information, see Introduction to pthread_exit. int pthread_attr_init (pthread_attr_t * attr): Initialize the attributes of a thread. for details about thread attributes, refer to thread attributes pthread_attr_t introduction int pthread_attr_destroy (pthread_attr_t * attr);: destroy a thread attribute pthread_attr_t and make it unusable before re-initialization. Int pthread_kill (pthread_t thread, int sig);: sends a signal to the thread. use the signal function in the created thread to set the signal processing function. For example, if you want to set the signal processing function of SIGKILL to sig_handler, you can write signal (SIGKILL, sig_handler) in this way ). Note. If you send SIGQUIT to a thread but the thread does not implement the signal processing function, the whole process exits. For more information about the signal processing mechanism, see
Signal Processing Mechanism Iv. synchronous FunctionsThe following functions use mutex and condition variables to synchronize threads. pthread_mutex_init (): Initialize the mutex lock pthread_mutex_destroy () Delete the mutex lock pthread_mutex_lock (): occupies the mutex lock (blocking operation (): attempts to occupy the mutex lock (non-blocking operation ). That is, when the mutex lock is idle, it occupies the lock; otherwise, return immediately. Pthread_mutex_unlock (): Release the mutex pthread_cond_init (): Initialize the condition variable pthread_cond_destroy (): destroy the condition variable pthread_cond_signal (): Wake up the first call to pthread_cond_wait () the sleeping thread pthread_cond_broadcast (): attempts to wake up all the threads that call pthread_cond_wait () on the condition variable and enter the sleeping thread pthread_cond_wait (): waiting for the special condition of the condition variable to happen. For more information, see Introduction to pthread_cond_signal and pthread_cond_wait and the differences between Mutex and Mutex. 5. Special Thread dataThe following function is used to implement the Thread-specific data (Thread-local storage) pthread_key_create (): Assign the key pthread_setspecific () to identify the Thread-specific data in the process (): set the thread-specific binding pthread_getspecific () for the specified thread-specific data key: Get the Key Binding of the call thread, and store the binding in the position pointed by the value pthread_key_delete (): destroy the specific data key of an existing thread. For more information, see Posix thread private data. Vi. Tool FunctionsInt pthread_equal (pthread_t threadid1, pthread_t thread2) checks whether two thread IDs are equal, returns 0 unequal, non-zero equal. Pthread_detach (): separates threads. for more information about this, see pthread_attr_t introduction to thread attributes pthread_self (): Get the thread ID (ID number) in the thread. The thread ID type is pthread_t, since threads in Linux adopt the POSIX standard, the pthread_t type is different in different systems. For example, in iistn, it is an unsigned long type, while in solaris, is of the unsigned int type. The structure pointer is used on FreeBSD. Therefore, you cannot directly use = for interpretation, but use pthread_equal for judgment.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.