I. Concept
First, Linux does not have real threads, and Linux threads are simulated using processes. When we need to run more than one execution stream at a time in a process, instead of opening up multiple processes to perform our operations (each process in a 32 -bit machine thinks it has a 4G of memory), it introduces threads, such as when we need to download content and browse the Web, At this point multithreading will play a role. A thread is the basic unit of a schedule, a process can have more than one thread, and its execution is more detailed than the process, and thread resources are shared.
two. Features
Because multiple threads of the same process share the same address space, so code snippets, data segments are shared, and if you define a function (stored in a code snippet), each thread can invoke it, and if you define a global variable (stored in a data segment) that can be accessed in every thread, The threads also share the following process resources and environments:
1. File Description Chart
2. How each signal is processed (SIG_IGN,SIG_DFL, user defined)
3. Current working directory
4. User ID and Group ID
But some resources are thread- exclusive :
1. Thread ID
2. Context, including the values of various registers, program counters and stack pointers
3. Stack space
4.errno variable
5. Signal Shielding Word
6. Scheduling priority
three. Simple and practical threading
1. Creating Threads
int Pthread_create (pthread_t *thread, const pthread_attr_t *attr, void * (*start_routine) (void *), void *arg);
Return value: Successfully returns 0, failure returns the error number. After you have created a new thread in one thread, the current thread returns from Pthread_create () and continues to execute, and the code executed by the new thread is passed by us to the pthread_create's function pointer start_routine decision. \ pthread_create
The thread parameter Id,void (star_routine) (void) of incoming threads is written by the user.
2. Thread Termination
The void Pthread_exit (void *retval) function is used for process termination, passing in an ID, calling exit (), and the main process terminates. Terminating a thread consists of three methods:
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.
2. A thread can invoke Pthread_cancel to terminate another thread in the same process.
3. Threads can tune pthread_exit to their end.
RetVal is a void * type, and other threads can tune pthread_join to obtain this pointer.
3. Thread Waiting
int Pthread_join (pthread_t thread,void **retval); successfully returns 0, failure returns the error number. A thread waits in a blocking wait, and a thread does not wait to produce a memory leak (a zombie process similar to a process)
4. Process cancellation
Within a reasonable range, threads can be self-contained or canceled by others. Returns pthread_canceled after cancellation (it is defined as (void *)-1), canceling the call function Pthread_cancel (ID);
5. Thread Separation
At any point in time the thread is either associative (joinable) or detached (detached). A thread that can be combined can be retrieved by other threads and killed by its resources. Its memory resources, such as stacks, are not released until they are reclaimed by another thread. In contrast, a detached thread cannot be recycled or killed by another thread, and its memory resources are automatically released by the system at the end of it.
By default, a thread can be combined, and each thread that can be combined should be explicitly reclaimed, calling the Pthread_join () function and separating the calling function Pthread_detach. The detached function is non-blocking and can be returned immediately. But why should it be separated. Because the main process handles threads more than one, each one needs to be waited, but the join is blocked, so that only one thread can be processed, so that multiple threads can be processed. When Pthread_detach () is invoked, the state of these child processes is set to detach, and the end of the thread runs automatically to free all resources.
The following is the test code:
#include <stdio.h> #include <stdlib.h> #include <pthread.h> void* thread1 (
{Pthread_detach (pthread_self ());//can still wait for printf after separation ("pid is:%d, Tid is:%d\n", Getpid (), pthread_self ());
Return (void*) 1;
int main () {pthread_t tid;
void *ret;
int err = pthread_create (&tid, NULL, THREAD1, NULL);
if (Err!= 0) {perror ("pthread_create\n");
return err; //If you run the wait code directly, you will generally wait for success and return 1//If you have to join the cancellation before waiting.
Wait for error, return-1//Pthread_cancel (TID);
The thread can cancel itself or be canceled, the thread terminates//calls Pthread_exit (TID), and the same usage is canceled.
int tmp = Pthread_join (tid, &ret);
if (TMP = = 0) {printf ("Wait success\n");
else {printf ("Wait failed\n");
printf ("pid is:%d, Tid is:%d\n", Getpid (), pthread_self ());
Sleep (1);
return 0; }