Thread Learning Notes

Source: Internet
Author: User

1. What is a thread?

The teacher said that the most important three concepts to learn the operating system (Operating system) are file, virtual memory, and process. The process has been learned before, so the concept of threading is better understood.

A process is an execution entity, and the operating system allocates resources on a per-process unit. In one execution space, multiple small processes can be used concurrently to accomplish different tasks, a small process called a thread. Process is a relatively large concept, the thread is a relatively materialized small concept, such as a process needs to complete a task, read the data received by the user, the data is sorted, and then the data output, this is a process to complete the task, the process can have three threads, which are responsible for reading the data, Data sorting, output data. From the above example we can see that the operating system is really responsible for the execution of the thread, the process is only a resource allocation entity.

2. Advantages of threading

Let's compare the threads and processes, thread-to-process, as the process is to the operating system. From the operating system level, the entity being executed is a process, and after the concept of threading is introduced, the entity being executed is a thread. It can be understood that they are all executing entities.

Relative to the process, the biggest advantage of the thread is the strong parallelism, in a process space can be multiple threads concurrently, greatly improving the efficiency of the program. In summary, there are two advantages:

1. Threads share all the resources in the process address space, so communication between threads is convenient, and the same task, if used in a multi-process programming model, must use inter-process communication before it is much less efficient.

2. Multi-threaded processing of different tasks, improve program concurrency, improve the efficiency of the program. In a sort instance, the data to be sorted is too large to read into the memory operation, it is necessary to read the data into the memory, then sort, and then output such a loop, most of the time wasted on the read and output, if the multi-process programming, a process responsible for reading data, a process responsible for sorting, A process is responsible for outputting the data, which can improve the efficiency a lot.

3. Thread identifiers

Like a process, a thread has its own ID, the data type is pthread_t, and is essentially an unsigned shape. About the thread ID there are two functions to know, pthread_self () function function is to get thread ID function, pthread_equal (pthread_t tid1, pthread_t tid2) The function is to compare the TID1 and Tid2 two thread IDs for equality, the equality function returns 0, not to return non-0 values.

4. Create a thread

(1) Create thread

Linux environment using pthread_create (pthread_t *restrict TIDP, const pthread_attr_t * Restrict attr, void * (* START_RTN) (void *), void(*restrict arg)) function creates a thread.

header file: #include <pthread.h>

parameter Description: pthread_t *restrict TIDP : Pointer to the thread identifier, when the function returns, TIDP points to the ID assigned to the thread by the kernel;

Const pthread_attr_t * Restrict attr : Sets thread properties, which are generally set to NULL if you do not want to specifically specify thread properties;

void * (*START_RTN) (void *) : The starting address of the thread run function, Start_rtn is a function pointer, the return value of the pointer to the function is a type of void pointer, The function parameter is also a pointer of type void;

void(*restrict arg) : The argument that runs the function start_rtn is a pointer of type void.

return Value: The successful creation process returns a value of 0, the failure returns an error number, and the perror () function maps the number to a descriptive language that is easy to understand.

function Function: creates a new thread and indicates the thread properties. Executes a thread body function while creating a new thread. The thread body function is tailored to your needs.

Sample program: Create a thread and print its thread

#include <stdio.h>#include<stdlib.h>#include<pthread.h>/*thread functions, print process ID and thread ID*/void* THFN (void*Arg)                     {pid_t pid; /*Process ID*/pthread_t Tid; /*Thread ID*/PID= Getpid ();/*Get Process ID*/Tid= Pthread_self ();/*Get thread ID*/       /*converting two IDs to unsigned integer printing*/printf ("The new thread:pid is:%u, Tid is:%u\n", (unsignedint) PID, (unsignedint) tid); returnNULL;}intMainvoid) {pid_t pid; interr;       pthread_t Tid, Mtid; PID=Getpid (); Mtid= Pthread_self ();/*gets the thread ID of the main thread*/Err= Pthread_create (&tid, NULL, THFN, NULL);/*Create thread if error err Save error number*/       /*The error number, like errno, can be mapped by the strerror function to a descriptive language that is easy to understand*/       if(Err! =0){              /*You cannot use the Perror function because the error number is not in the errno variable*/printf ("can ' t create thread%d\n", Strerror (err)); Exit (1); }       /*hibernate for one second to ensure that newly created threads are called before the main thread*/Sleep (1); /*Print the main thread ID and process ID*/printf ("The main thread:pid is:%u, Tid is:%u\n", (unsignedint) PID, (unsignedint) Mtid); return 0;}

Thread Learning Notes

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.