Pthread series functions and simple multi-threaded server-side programs
First, POSIX thread overview
We know that processes are running in separate address spaces, and that sharing data between processes requires interprocess communication, in some cases requiring simultaneous execution of multiple control processes in a single process, where threads are used, such as downloading software for a graphical interface that requires interaction with the user, Wait and handle the user's mouse keyboard events, on the other hand, need to download multiple files at the same time, waiting for and processing data from multiple network hosts, these tasks require a "wait-process" loop, you can use multithreading, a thread dedicated to interacting with the user, Several other threads are responsible for communicating with one network host per thread.
As we've said before, main functions and signal processing functions are multiple control processes in the same process address space, multithreading is also the case, but more flexible than the signal processing function, signal processing function control flow is only in the signal delivery, after processing the signal is finished, and multithreading control process can coexist for a long time , the operating system schedules and toggles between threads, just like scheduling and switching between multiple processes. Because multiple threads of the same process share the same address space, the Textsegment and Data segment are shared, and if a function is defined, it can be invoked in each thread, and if a global variable is defined, it can be accessed in every thread, and in addition, The threads also share the following process resources and environments:
File Description Tabulation
How each signal is processed (sig_ign, SIG_DFL, or custom signal processing functions)
Current working directory user ID and group ID
But some of the resources are one for each thread:
Thread ID
Context, including values for various registers, program counters, and stack pointers
Stack space
errno variable
Signal Shielding Word
Scheduling priority
The line threading function we're going to learn is defined by the POSIX standard, called POSIX thread or pthread. The Linux thread function is located in the Libpthread shared library, so add the-lpthread option at compile time.
Second, Pthread series functions
A
Function: Create a new thread
Prototype int pthread_create (pthread_t *thread, const pthread_attr_t *attr, void * (*start_routine) (void*), void *arg);
Parameters
Thread: Returning Threads ID
attr: Set the properties of the thread, attr NULL to use default properties
Start_routine: is a function address, the function to execute after the thread starts
ARG: Parameters passed to thread start function
Return value: Successful return of 0; failure return error code
Error checking:
Previously learned system functions are successfully returned 0, the failure returns-1, and the error number is stored in the global variable errno, and the function of the Pthread library returns the error number through the return value, although each thread has a errno, which is provided for compatibility with other function interfaces. The Pthread library itself does not use it, and it is clearer to return the error code by return value. Because the Pthread_create error code does not exist in the errno, you cannot print the error message directly with Perror (3), you can use Strerror (3) to convert the error code to the error message and then print.
Two
Function: Thread termination
prototype void pthread_exit (void *value_ptr);
Parameters
Value_ptr:value_ptr do not point to a local variable because the thread function has exited when the other thread gets the return pointer.
Return value: No return value, same as process, unable to return to its caller (itself) at the end of the thread
If you need to terminate only one thread without terminating the entire process, there are three ways:
1, return from the thread function. This method does not apply to the main thread, and return from the main function is equivalent to calling exit, and if any one of the threads calls exit or _exit, all threads of the entire process are terminated.
2, a thread can invoke Pthread_cancel to terminate another thread in the same process.
3, the thread can call pthread_exit terminate itself.
Three
function: Wait for thread to end
Prototype int pthread_join (pthread_t thread, void **value_ptr);
Parameters
Thread: Threads ID
Value_ptr: It points to a pointer, which points to the return value of the thread
Return value: Successful return of 0; failure return error code
When the Start_routine in Pthread_create returns, the thread exits, and other threads can invoke Pthread_join to get start_routine return value, similar to the parent process call Wait (2) to get the child process exit status.