Linux application development (12): multi-thread programming

Source: Internet
Author: User

I. chicken ribs:

1. Thread theory basis

(1) Reasons for thread introduction:

In Linux, to start a new process, you must allocate it an independent address space and create a large number of data tables to maintain its code segments and stack segments, therefore, this multi-task method is costly.

(2) Advantages of multithreading:

I. the overhead of Inter-thread switching is much smaller than that of inter-process switching. According to statistics, the gap is about 30 times.

Ii. Inter-thread communication is easier than inter-process communication. Different processes have their own independent process spaces. data transmission between them can only be performed through inter-process communication. This method is time-consuming and labor-consuming. Different threads in the same process share the address space and data space, so the data of one thread can be directly used by other threads. This method is fast and simple.

2. multithreading in Linux

(1) multithreading in Linux follows the POSIX thread interface, which is called pthread. When writing multi-threaded programs in Linux, you need to use the header file pthread. h. The link is to use the library libpthread. h


Ii. Multi-Thread Programming

1. Creation thread: int pthread_create (pthread_t * TIDP, const pthread_attr_t * ATTR, void * (start_rtn) (void), void * Arg );

(1) parameters:


= TIDP: thread ID


= ATTR: thread attribute (usually empty)


= Start_rtn: the function to be executed by the thread


= Arg: start_rtn Parameter

(2) Note:

After a thread is created using the pthread_join () method, the thread is in the ready state, not the execution state ". Therefore, after the current function executes this method, it will directly execute the following code until thread switching occurs.

2. multi-threaded program Compilation:

Because the pthread library is not a Linux library, you must add-lpthread, eg: # GCC filename-lpthread during compilation.

3. Thread wait: int pthread_join (pthread_t tid, void ** rval_ptr );

(1) cause of Introduction: when the parent process waits for the child process, we use the wait () function. If the thread waits for another thread, we can use the pthread_join () function. Function: blocks the current calling thread until the specified thread is terminated.

(2) parameters:


= TID: ID of the thread waiting to exit


= Rval_ptr: The returned values after the called thread exits will be stored in rval_ptr

(3) Note:

The main function is a process. After the main function is executed, the entire process is executed. At this time, the main function exits regardless of whether there are any sub-threads being executed.

4. Thread ID (return thread ID): pthread_t pthread_self ();

Note: The thread ID here is equal to the process ID.


5. Clear

(1) There are two cases of thread termination:

I. Normal termination: When a thread actively calls pthread_exit or returns from the thread function, the thread Exits normally.

Ii. Abnormal Termination: the exit mode is not met when the thread is intervened by other threads or because of an error in its own operation (such as access to an invalid address.

(2) Whether it is the possible thread termination or abnormal termination, there will be a problem of resource release. Therefore, how to ensure that the resources occupied by the thread can be smoothly released upon termination is a problem that must be solved.

(3) The termination action (including calling pthread_exit () and abnormal termination, excluding return) in the program section (temporarily called protected area code) between pthread_cleanup_push () and pthread_cleanup_pop will execute the function: the cleanup function specified by pthead_cleanup_push.

I, void pthread_cleanup_push (void (* RTN) (void *), void * Arg );

Parameters:

A. RTN: clearing the Function

B. Arg: Clear the parameters to be used by the function.

Ii. Void pthread_cleanup_pop (INT execute );

Function: When the code in the protected area is not terminated, the pop function will pop up the clear function specified by the push function to clear the stack.

Parameters:

The execute parameter is used to determine whether to execute the cleanup function while executing the pthread_cleanup_pop () function,

Execute, = non-0: Execute, = 0: do not execute.













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.