11.1: Introduction
This chapter describes the use of multithreading
11.2: Thread Concept
A typical UNIX process can be thought of as having only one control thread: a process that only does one thing at a time.
11.3: Thread Identification
Each thread is identified by a thread ID, just like a process.
#include <pthread.h>int// determine if two thread IDs are equal, if equal, return 0, otherwise return 0pthread_t pthread_self ( void// returns the thread ID of the calling thread
11.4: Thread Creation
The default one process contains only one thread, if you want to implement multi-threading, you first have to create a new thread, you can use the function pthread_create to implement.
#include <pthread.h>// returns error number intconstvoid* If successful 0 is returned (*start_ RTN) (voidvoid *restrict arg);
If successful, TIDP points to a memory-saving thread id,attr is a thread property, which can be a thread function for NULL,START_RTN, and ARG is a parameter passed to the thread function.
Example: 11.1 Test thread ID
11.5: Thread Termination
If any thread in the process calls exit, _exit, _exit, the entire process terminates. Similarly, if the default action of the signal is to terminate the process, the signal sent to the thread terminates the entire process.
A single thread can exit in the following three ways, stopping its control flow without terminating the entire process:
1. The thread is simply returned from the startup routine, and the return value is the thread's exit code.
2. A thread can be canceled by another thread in the same process.
3. Thread calls Pthread_exit ().
#include <pthread.h>void pthread_exit (void *rval_ptr);
Rval_ptr is an untyped pointer similar to a single parameter passed to the start routine. Other threads in the process can call Pthread_join to access the pointer.
#include <pthread.h>intvoid **rval_ptr); Returns the error number if 0 is returned successfully
The calling thread will block until the specified thread calls Pthread_exit (), returns from the startup routine, or is canceled.
Example: 11-2 getting thread exit status
Example: Incorrect use of the 11-3 pthread_exit parameter
Threads can request cancellation of other processes in the same process by calling the Pthread_cancel function.
#include <pthread.h>int// If successful returns 0, otherwise the error number is returned
Note: Pthread_cancel does not wait for the thread to terminate, it only requests.
A thread can schedule a function that needs to be called when it exits, similar to the function that a process needs to call when it can schedule a process to exit with the Atexit function. Such a function is known as the thread cleanup handler (threads cleanup handler). A thread can establish multiple cleanup handlers. Handlers are recorded in the stack, meaning they are executed in the opposite order as they were registered.
#include <pthread.h>void pthread_cleanup_push ( Void (*RTN) (voidvoid * ARG); void pthread_cleanup_pop (int execute);
When the thread executes the following action, the cleanup function is called, the invocation parameter is ARG, and the order of the cleanup function Rtn is arranged by the Pthread_cleanup_push function.
1. When Pthread_exit is called.
2. When the request is canceled accordingly.
3. When calling Pthread_cleanup_pop with a non-0 execute parameter.
If the Execute parameter is set to 0, the cleanup function will not be called. In either case, Pthread_cleanup_pop deletes the cleanup handler that was established by the last Pthread_cleanup_push call.
Example: 11-4 thread Cleanup Handler
By default, the terminating state of a thread is saved to call Pthread_join on that thread, and if the thread is already detached, the thread's underlying storage resource can be reclaimed as soon as it is terminated. When a thread is detached, it cannot wait for its terminating state with the Pthread_join function. A pthread_join call to a disconnected thread will produce a failure and return einval. The Pthread_detach call can be used to get the thread into a detached state.
#include <pthread.h>int pthread_detach (pthread_t tid);
Detach state-that is, the thread can reclaim its resources when it terminates.
11.6: Thread Synchronization
1. Mutex Amount
2. Avoid deadlocks
3. Read/write Lock
4. Condition variables
11.7: summary
This chapter introduces the concept of threading, discusses the existing POSIX primitives for creating threads and destroying threads, introduces thread synchronization, discusses three basic synchronization mechanisms: mutexes, read-write locks, and condition variables to learn how to use them to protect shared resources.
11th Chapter: Threading