Pthread Thread Programming-POSIX thread Mechanism

Source: Internet
Author: User

In Linux, the concept and processing of threads and processes are not strictly differentiated like other operating systems. in Linux, a thread is also called a lightweight process. in addition, the important difference between windows and Windows systems is that in Linux, the new thread is not in the original process/thread, but called through the clone () system, generate a process/thread that is exactly the same as the original process/thread, and execute the thread function in this thread.

POSIX Thread Programming mainly involves POSIX thread interfaces and related thread communication mechanisms.
The following describes the POSIX thread interfaces, thread communication mechanisms, and some corresponding struct, which can be found in the Linux kernel source code:
In the/usr/include/pthread. h file
Extern int pthread_create (pthread_t * newthread, const pthread_attr_t * ATTR, void * (void *), void * Arg );
Function: Create a New thread;
The first parameter indicates the thread ID that uniquely identifies a thread;
Second parameter: attributes of the new thread;
Third parameter: The function running in the new thread;
Fourth parameter: The parameter passed to the function.

The struct is declared in the same file as the function:
Typedef unsigned long int pthread_t;
Typedef Union
{
Char size [pthread_attr_t];
Long int align;
} Pthread_attr_t;
// This is the struct used by the thread attribute. Set this struct if you need any attributes of the newly created thread.

Extern int pthread_join (pthread_t ID, void ** thread_return );
Function: wait for the end of a thread.
The first parameter is the ID of the thread to wait for completion;
The second parameter: a user-defined pointer that stores the return value of a thread.
Note: One thread cannot be waited by multiple threads.

Extern void pthread_exit (void * retval );
Function: exit a thread.
The first parameter: record the return code of the function.

Extern pthread_t pthread_self (void );
Function: Obtain the thread ID.

The above are commonly used POSIX thread interfaces, such as other functions such as pthread_cancel () and pthread_detach (). If you are interested, go and have a look.

Let's talk about thread communication:
In Linux, there are three main methods of thread communication: mutex lock, conditional variable and semaphore.
1. mutex lock:
The struct name is pthread_mutex_t. It is a long struct and I will not list it. If you want to see it, you know it...
The pthread Interface related to the mutex lock is:
Pthread_mutex_init ();
Pthread_mutex_lock ();
Pthread_mutex_unlock ();
Pthread_mutex_destroy ();

Lock attributes: pthread_mutexaddr_t struct. If you want to configure the lock attributes, set the struct variable.

2. Condition variables:
Struct name: pthread_conf_t,
Related interface functions are:
Pthread_conf_init ();
Pthread_conf_wait ();
Pthread_conf_signal ();
Pthread_conf_broadcast ();

Attribute configuration of the condition variable: pthread_confattr_t struct. If you want to set the attribute of the corresponding variable, set this variable.

3. Signal
Typedef Union
{
Char _ SIZE [size_of_sem];
Long int _ align;
} Sem_t;
It is the structure Declaration of the signal, and there is no sem_t attribute declaration, because the form of sem_t is exactly the same as the structure form of the above two attributes.
The sem_t-related interfaces are as follows:
Sem_init ();
Sem_post ();
Sem_wait ();
Sem_destroy ();
The sem_t interface is in/usr/include/semaphore. h.

The above is the knowledge of some POSIX interfaces. You can view relevant information on your own.

We will discuss thread communication in detail later.

Thanks.

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.