LINUX Study Notes 17-Multithreading

Source: Internet
Author: User

1. Thread theoretical basis:
 
A) more frugal than processes: All code data in the thread is shared.
 
B) Convenience: convenient communication because of data sharing
 
C) follow the POSIX thread interface, called pthread, which requires # include <pthread. h>
 
2. Creation thread: int pthread_create (pthread_t * tidp, const pthread_attr * attr, void * (start_rtn) (void), void * arg)
 
A) Tidp: used to obtain the ID of the Creation thread
 
B) Attr: Specifies the thread attribute, which is generally NULL.
 
C) Start_rtn: the function to be executed by the thread. After the function is executed, the thread is finished!
 
D) Arg: start_rtn parameter. Note that it is a null pointer variable, which requires forced type conversion.
 
E) Compile: The Pthread library is not a LINUX library and must be added during compilation,
 
1. # gcc filename-lpthread: l specify the Library added by myself
 
F) Note: threads share global variables and data segments first! Local variables are on the stack and are not shared.
 
3. Thread termination:
 
A) The program is returned from the startup process.
 
B) The thread can be terminated by another process. The thread creation process ends, and the thread ends.
 
C) The thread calls the pthread_exit function by itself: void pthread_exit (void * rval_ptr)
 
1. # include <pthread. h>
 
2. rval_ptr: gets the pointer to the return value of the thread exit.
 
4. Thread wait: int pthread_join (pthread_t tid, void ** rval_ptr)
 
A) Role: Wait for a thread to end before the main program.
 
B) # include <pthread. h>
 
C) Tid: thread ID
 
D) Rval_ptr: indicates the pointer returned by the thread. It depends on whether you need to return the value. NULL is not required.
 
E) Note: After a thread is created, it is in the ready state. The main process must have a thread wait time before the thread can have time to run.
 
5. Obtain the thread ID: pthread_t pthread_self (void)
 
A) # include <pthread. h>
 
6. Exit protection:
 
A) when the program unexpectedly exits, the stack space is cleared.
 
B) void pthread_cleanup_push (void (* rth) (void *), void * arg)
 
1. # include <pthread. h>
 
2. Use the push function and pop to enclose the program segments.
 
3. Press the cleanup function into the cleanup stack. When the included program does not run to POP and exits, the cleanup function is called, excluding return. If there are multiple PUSH entries
 
4. Rtn: clearing the Function
 
5. Arg: Clear function parameters
 
C) void pthread_cleanup_pop (int execute)
 
1. # include <pthread. h>
 
2. Clear the stack in the clear function pop-up window.
 
3. Execute: determines whether to Execute this function while the cleanup function is popped up. If it is not 0, Execute the function. If it is 0, do not Execute the function.
 
D) clearing function: the content defined by myself. The most important thing is to have a return statement.

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.