Linux thread Learning (1)

Source: Internet
Author: User

The learning materials are from apue, a famous book, and some are from Wikipedia. I will share what I learned step by step!

The overall content provided first

1) Basic Concepts

Thread, also known as a lightweight thread,YesProgramThe smallest unit of the execution stream! Processes are divided into user-level threads and kernel-level threads!

Common models include:

Thread: Process

1: 1 refers to the kernel-level thread, on Linux usual C library implement this approach!

N: 1 indicates a user-level thread! An N: 1 model implies that all application-level threads map to a single kernel-level scheduled entity; the kernel has no knowledge of the application threads. with this approach, context switching can be done very quickly and, in addition, it can be implemented even on simple kernels which do not support threading

For user-level threads, the advantage is the ability to quickly perform context switch conversion, because they do not enter the kernel mode! The disadvantage is that hardware cannot be used for acceleration! Because there can only be one thread at a time

Can be called by processor!

2) Features

Threads are further subdivided into processes to improve concurrent execution and execution efficiency. Threads share process resources. A thread can be created or abolished.

In a thread, context switch is much faster than inter-process switch. A thread contains necessary system resources, such as a PC, some required registers, stacks, and thread IDs!

3) differences with processes

1) The process contains an independent address space, but the thread does not exist. The thread must be in the process! The thread must rely on the thread to survive. A process can be considered as an independent thread.

2) The thread contains a small amount of necessary status information, while the process contains a large amount of information!

3) context switch in the process is much slower than context switch in the process!

4) thread creation and Termination

First, we will introduce several important functions.

The header files of these functions are # include <pthread. h>

Int pthread_equal (pthread_t tid1, pthread_t tid2) // compare whether the two thread IDs are equal

Pthread_t pthread_self (void) // The returned thread ID is

Int pthread (pthread_t * restrict TIDP, const pthread_attr_t * restrict ATTR, void * (* start_rtn) (void), void * restrict Arg); // create an ID

Provide a paragraphCodeFor reference!

 1 # Include <pthread. h> 2 # Include <stdio. h> 3   Void * Func ( Void * Arg)  4   {  5 Printf ( "  The PID of the programming is % d \ n  " , Getpid ());  6 Printf ( "  The new thread is: % u \ n  "  , Pthread_self ());  7   Return (( Void *) 0  );  8   }  9   Int  Main () 10   {  11 Pthread_t tid = Pthread_self (); // TID is the ID of the main thread.  12      Pthread_t ntid;  13 Printf ( "  The main thread is % u: \ n  "  , Tid );  14 Pid_t pid = Getpid ();  15 Printf ( " The PID of the process is % d: \ n  "  , Pid );  16    Int Err = pthread_create (& Ntid, null, func, null );  17      If (Err! = 0  )  18 Printf ( "  Create thread error \ n  "  ); 19 Sleep ( 1  );  20   Return   0  ;  21   22 }

From this program, I can see that the newly created thread and the previous thread are in the same process, but their thread IDs are different!

Thread termination:

1) You cannot use common functions, such as _ exit and exit. Using these functions in any thread will terminate the process.

2) There are three methods to terminate a thread

1. The thread is canceled by another thread.

2. The thread calls the pthread_exit () function.

3. The thread returns the exit code of the thread from the startup routine!

We usually use the pthread_join () function to retrieve the exit code of the thread.

A simple example is provided.

 1 # Include <pthread. h> 2 # Include <stdio. h> 3   Void * Func ( Void * Arg)  4   {  5 Printf ( "  The thread1 exit \ n  "  ); 6 Pthread_exit (( Void *) 1  );  7   }  8   Void * Func1 ( Void * Arg)  9   {  10 Printf ( "  The thread2 exit \ n  " );  11 Pthread_exit (( Void *) 2  );  12   }  13   Int  Main ()  14   {  15           Int  Err;  16   Pthread_t tid1, tid2; 17           Void * Tret;  18 Err = pthread_create (& Tid1, null, func, null );  19           If (Err! = 0  )  20 Printf ( "  Can't create thread 1: \ n  "  );  21 Err = pthread_create (& Tid2, null, func1, null );  22           If (Err! = 0  )  23 Printf ( "  Can't create thread 2: \ n  "  );  24 Err = pthread_join (tid1 ,& Tret );  25           If (Err! =0  )  26 Printf ( "  Can't join thread1 \ n  "  );  27 Printf ( "  Thread1 exit code % d \ n  " ,( Int  ) Tret );  28 Err = pthread_join (tid2 ,& Tret ); 29           If (Err! = 0  )  30 Printf ( "  Can't join thread2 \ n  "  );  31 Printf ( "  Thread2 exit code % d \ n  " ,( Int  ) Tret );  32          Return   0  ;  33   34   35   36   37 }

Note that the GCC-o...-lpthread is used during compilation.

5) thread synchronization will be explained in the next article!

 

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.