The basic concept of multithreaded programming

Source: Internet
Author: User
Tags mutex semaphore switches

First, what is a thread

A thread is a control flow in a single order in a process. Also known as light weight process (lightweight processes). Computer Science terminology refers to the scheduling unit of a running program.

A standard thread consists of a thread ID, a current instruction pointer (PC), a collection of registers, and a stack. In addition, the thread is an entity in the process, is the basic unit that is dispatched and dispatched by the system independently, the thread does not own the system resources, only has a bit of resources that are essential in the operation, but it can share all the resources owned by the process with other threads belonging to one process. One thread can create and revoke another thread, which can be executed concurrently between multiple threads in the same process.

In a multithreaded operating system, it is common to include multiple threads in a process, each as the basic unit of CPU utilization, and an entity that spends the least cost. Threads have the following properties:
(1) Light body
Entities in a thread do not have system resources at all, but have a bit of an essential resource that can be run independently, for example, in each thread, there should be a thread control block TCB for controlling the thread to run, a program counter that indicates the sequence of instructions being executed, a reserved local variable, A set of registers and stacks for a few state parameters and return addresses.
(2) The basic unit of independent Dispatch and dispatch
In multi-threaded OS, threads are the basic units that can run independently, and thus are the basic units of independent Dispatch and dispatch. Because the thread is "light", the thread switches very quickly and with little overhead (in the same process).
(3) can be executed concurrently
In a process between multiple threads, can be executed concurrently, and even allow all threads in a process to execute concurrently, also, the threads in different processes can execute concurrently, take full advantage of and play the processor and peripheral equipment to work in parallel.
(4) Sharing process resources
Each thread in the same process can share the resources owned by the process, which begins with the following: All threads have the same address space (the process's address space), which means that the thread can access every virtual address in that address space, and access to the open files, timers, Semaphore agencies, and so on. Because threads in the same process share memory and files, there is no need to call the kernel to communicate with one another.

There are several differences between threads compared to processes:

(1) address space and other resources : processes are independent of each other and shared among threads of the same process. Threads within a process are not visible in other processes.

(2) Communication: Inter-process communication requires IPC, between threads can directly read and write process data segments (such as global variables) to communicate (requires process synchronization and mutual exclusion means of support to ensure data consistency).

(3) Scheduling and switching : Thread context switches are much faster than process context switches.

(4) The process is an independent unit of system resource allocation and scheduling, the thread is an entity of the process, is the basic unit of CPU dispatch and dispatch, it is smaller than the process can run independently of the basic unit.

Second, multi-threaded programming

Because of the mutual constraints between threads, the thread is running in a discontinuous. Threads also have three basic states of readiness, blocking, and running. As a result, several threads can be created in a process to improve the efficiency of the execution of the program, and some programs also execute multiple different code modules at the same time by employing multithreading techniques. However, when multiple threads are running at the same time, the thread function may be called, and when multiple threads write to the same memory address at the same time, the write data will be overwritten multiple times due to CPU time scheduling problems, so the thread should be synchronized.

  thread synchronization , that is, when one thread is operating on the memory, no other thread can manipulate the memory address until the thread completes the operation, and the other thread can manipulate the memory address while the other threads are waiting.

There are many ways to implement thread synchronization, such as critical sections (critical section), Events (event), semaphores (semaphore), mutexes (mutexes), and so on.
1, critical area: through the serialization of multithreading to access public resources or a piece of code, fast, suitable for controlling data access. Only one thread is allowed to access the shared resource at any time, and if more than one thread attempts to access the public resource, the other threads that attempt to access the public resource will be suspended after one thread enters, and wait until the thread that enters the critical section leaves and the critical section is freed, and the other thread can preempt;
2, Mutex: adopt mutually exclusive object mechanism. Only the thread that owns the mutex has access to the public resource, because there is only one mutex object, so that the public resources are not accessed by multiple threads at the same time. Mutual exclusion can not only realize the common resources security sharing of the same application, but also realize the security sharing of public resources of different applications.
3, Semaphore: It allows multiple threads to access the same resource at the same time, but needs to limit the maximum number of threads accessing this resource at the same time;
4, Event: the way to maintain the synchronization of threads by notification operation, it is also convenient to achieve the priority comparison of multiple threads operations.

Iii. why multi-threaded programming is required

There are many reasons to require multi-threaded programming, and we can solve them by example:
(1) Some to improve the speed of operation, such as multi-core CPU multi-threading, can make full use of multiple core simultaneous processing tasks;
(2) Some in order to improve the utilization of resources, such as downloading resources in the network environment, the delay is often very high, we can use different thread from different places to obtain resources, which can improve efficiency;
(3) Some in order to provide better services, such as the use of multiple threads on the server can handle different request tasks separately;
(4) Other places that require multi-threaded programming, etc...

The basic concept of multithreaded programming

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.