• 欢迎访问速搜资源吧,如果在网站上找不到你需要的资源,可以在留言板上留言,管理员会尽量满足你!

【速搜问答】多线程是什么

问答 admin 3年前 (2020-06-12) 507次浏览 已收录 0个评论

汉英对照:
Chinese-English Translation:

多线程是指从软件或者硬件上实现多个线程并发执行的技术。具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程,进而提升整体处理性能。具有这种能力的系统包括对称多处理机、多核心处理器以及芯片级多处理或同时多线程处理器。

Multithreading refers to the technology of realizing concurrent execution of multiple threads from software or hardware. Computers with multithreading capability can execute more than one thread at a time because of hardware support, thus improving the overall processing performance. Systems with this capability include symmetric multiprocessors, multicore processors, and chip level multiprocessors or simultaneous multithreading processors.

多线程(英语:multithreading),是指从软件或者硬件上实现多个线程并发执行的技术。具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程,进而提升整体处理性能。具有这种能力的系统包括对称多处理机、多核心处理器以及芯片级多处理(Chip-level multithreading)或同时多线程(Simultaneous multithreading)处理器。在一个程序中,这些独立运行的程序片段叫作“线程”(Thread),利用它编程的概念就叫作“多线程处理(Multithreading)”。

Multithreading refers to the technology of realizing concurrent execution of multiple threads from software or hardware. Computers with multithreading capability can execute more than one thread at a time because of hardware support, thus improving the overall processing performance. Systems with this capability include symmetric multiprocessors, multi-core processors, and chip level multithreading or simultaneous multithreading processors. In a program, these independently running program segments are called threads, and the concept of programming with them is called multithreading.

定义

definition

在计算机编程中,一个基本的概念就是同时对多个任务加以控制。许多程序设计问题都要求程序能够停下手

In computer programming, a basic concept is to control multiple tasks at the same time. Many programming problems require the program to stop

头的工作,改为处理其他一些问题,再返回主进程。可以通过多种途径达到这个目的。最开始的时候,那些掌握机器低级语言的程序员编写一些“中断服务例程”,主进程的暂停是通过硬件级的中断实现的。尽管这是一种有用的方法,但编出的程序很难移植,由此造成了另一类的代价高昂问题。中断对那些实时性很强的任务来说是很有必要的。但对于其他许多问题,只要求将问题划分进入独立运行的程序片断中,使整个程序能更迅速地响应用户的请求。

The first task is to deal with some other problems and then return to the main process. This can be achieved in many ways. At the beginning, the programmers who master the low-level language of the machine wrote some “interrupt service routines”, and the pause of the main process was realized through the hardware level interrupt. Although this is a useful method, it is difficult to transplant the program, which leads to another kind of expensive problem. Interruptions are necessary for tasks that are very real-time. But for many other problems, as long as the problem is divided into independent program segments, the whole program can respond to the user’s request more quickly.

最开始,线程只是用于分配单个处理器的处理时间的一种工具。但假如操作系统本身支持多个处理器,那么每个线程都可分配给一个不同的处理器,真正进入“并行运算”状态。从程序设计语言的角度看,多线程操作最有价值的特性之一就是程序员不必关心到底使用了多少个处理器。程序在逻辑意义上被分割为数个线程;假如机器本身安装了多个处理器,那么程序会运行得更快,毋需作出任何特殊的调校。根据前面的论述,大家可能感觉线程处理非常简单。但必须注意一个问题:共享资源!如果有多个线程同时运行,而且它们试图访问相同的资源,就会遇到一个问题。举个例子来说,两个线程不能将信息同时发送给一台打印机。为解决这个问题,对那些可共享的资源来说(比如打印机),它们在使用期间必须进入锁定状态。所以一个线程可将资源锁定,在完成了它的任务后,再解开(释放)这个锁,使其他线程可以接着使用同样的资源。

At first, threads were just a tool for allocating processing time for a single processor. But if the operating system itself supports multiple processors, then each thread can be assigned to a different processor, which really enters the “parallel operation” state. From the perspective of programming language, one of the most valuable features of multithreading is that programmers don’t have to care how many processors they use. The program is logically divided into several threads; if the machine itself has multiple processors installed, the program will run faster without any special adjustment. According to the previous discussion, you may feel that thread processing is very simple. But we must pay attention to one problem: sharing resources! If multiple threads are running at the same time, and they try to access the same resources, they run into a problem. For example, two threads cannot send information to a printer at the same time. To solve this problem, for shareable resources (such as printers), they must be locked during use. So a thread can lock resources. After finishing its task, it can unlock (release) the lock so that other threads can use the same resources.

多线程是为了同步完成多项任务,不是为了提高运行效率,而是为了提高资源使用效率来提高系统的效率。线程是在同一时间需要完成多项任务的时候实现的。

Multithreading is to complete multiple tasks synchronously, not to improve the efficiency of running, but to improve the efficiency of using resources to improve the efficiency of the system. Threads are implemented when multiple tasks need to be completed at the same time.

最简单的比喻多线程就像火车的每一节车厢,而进程则是火车。车厢离开火车是无法跑动的,同理火车也不可能只有一节车厢。多线程的出现就是为了提高效率。同时它的出现也带来了一些问题。

The simplest metaphor is that multithreading is like every carriage of a train, and the process is a train. The carriage can’t run without the train. Similarly, the train can’t have only one carriage. The emergence of multithreading is to improve efficiency. At the same time, it brings some problems.

用途

purpose

在大多数研究领域内是要求线程调度程序要能够快速选择其中一个已就绪线程去运行,而不是一个一个运行而降低效率。所以要让调度程序去分辨线程的优先级是很重要的。而线程调度程序可能是以硬件、软件,或是软硬件并存的形式存在。

In most research areas, it is required that the thread scheduler should be able to quickly select one of the ready threads to run, rather than one by one to reduce efficiency. So it’s important for the scheduler to prioritize the threads. The thread scheduler may exist in the form of hardware, software, or both.

而另一个研究领域则是要研究何种事件(高速缓存失败、内部运行连续性、使用 DMA 等)会造成线程切换。

Another research area is to study which events (cache failure, internal running continuity, using DMA, etc.) will cause thread switching.

如果多线程的方案会复制所有软件可见的状态,包括特许的控制登录、TLB 等,那就能够让虚拟机去创造各式线程。这样子就允许在相同的处理器中每个线程跑各自的操作系统。换句话说,如果只有存储了用户模式的状态,就能够让相同的裸晶大小的芯片在一段时间内处理更多的线程。

If the multithreading scheme can copy all visible states of software, including privileged control login, TLB, etc., then it can let the virtual machine create all kinds of threads. This allows each thread to run its own operating system in the same processor. In other words, if only the state of user mode is stored, the chip with the same size of naked crystal can process more threads in a period of time.

硬件支持

Hardware support

多线程硬件支持的目标,即支持快速进行就绪态线程、执行态线程间的切换。为达成这个目标,需要硬件实现保存、恢复程序看得见的寄存器以及一些对程序执行有影响的控制寄存器(如程序计数器 PC、程序状态寄存器 SR)。从一个线程切换到另一个线程对硬件来讲意味着保存当前线程的一组寄存器的值,并恢复即将执行线程的一组寄存器的值。

The goal of multithreading hardware support is to support fast switching between ready threads and execution threads. In order to achieve this goal, it is necessary to save and restore the visible registers of the program and some control registers (such as program counter PC and program status register SR) that affect the program execution. Switching from one thread to another means saving the value of a set of registers of the current thread and recovering the value of a set of registers of the thread to be executed.

新增这些功能的硬件有以下优势:

The hardware that adds these features has the following advantages:

线程切换能够在一个 CPU 周期内完成(有些硬件甚至没有开销,上个周期在运行线程 A,下个周期就已在运行线程 B)。

Thread switching can be completed in one CPU cycle (some hardware does not even have overhead. Thread a was running in the last cycle and thread B was running in the next cycle).

每个线程看起来就像是独自运行的,即没有与其他线程共享硬件资源。对操作系统来说,通常每个线程都被视做独占一个处理器,这样将简化系统软件的设计(尤其是对于支持多线程的操作系统)。

Each thread looks like it is running on its own, that is, it does not share hardware resources with other threads. For the operating system, each thread is usually regarded as an exclusive processor, which will simplify the design of the system software (especially for the operating system supporting multithreading).

为了在各个线程间有效率的进行切换,每个线程需要保存自己的一组寄存器集(register set)。有些硬件设计成每个处理器核心具有两组寄存器文件,以实现在多个线程间快速切换。

In order to switch between threads efficiently, each thread needs to save its own set of registers. Some hardware is designed to have two sets of register files in each processor core to realize fast switching between multiple threads.

进程与线程

Process and thread

英文:Process、Thread

English: process, thread

每个正在系统上运行的程序都是一个进程。每个进程包含一到多个线程。进程也可能是整个程序或者是部分程序的动态执行。线程是一组指令的集合,或者是程序的特殊段,它可以在程序里独立执行。也可以把它理解为代码运行的上下文。所以线程基本上是轻量级的进程,它负责在单个程序里执行多任务。通常由操作系统负责多个线程的调度和执行。

Every program running on the system is a process. Each process contains one or more threads. The process may also be the dynamic execution of the whole program or some programs. A thread is a set of instructions, or a special segment of a program, which can be executed independently in the program. It can also be understood as the context in which the code runs. So threads are basically lightweight processes, which are responsible for multitasking in a single program. The operating system is usually responsible for the scheduling and execution of multiple threads.

线程是程序中一个单一的顺序控制流程.在单个程序中同时运行多个线程完成不同的工作,称为多线程。

Thread is a single sequential control process in a program. Running multiple threads in a single program at the same time to complete different work is called multithreading.

线程和进程的区别在于,子进程和父进程有不同的代码和数据空间,而多个线程则共享数据空间,每个线程有自己的执行堆栈和程序计数器为其执行上下文.多线程主要是为了节约 CPU 时间,发挥利用,根据具体情况而定. 线程的运行中需要使用计算机的内存资源和 CPU。

The difference between a thread and a process is that a child process and a parent process have different code and data space, while multiple threads share data space. Each thread has its own execution stack and program counter as its execution context. Multithreading is mainly to save CPU time and make full use of it, depending on the specific situation. The memory resources and CPU of the computer need to be used in the running of the thread.

使用 Runnableclass MyThread{public static void main(String[] args){new Thread(new Runnable() {@Override    public void run() {//写上线程需要执行的代码}}).start();}}

Use runnableclass myThread {public static void main (string [] args) {new thread (New runnable() {@ override public void run() {/ / write the code the thread needs to execute}). Start();}}

这里推荐使用 Thread 类来实现多线程。

It is recommended to use thread class to implement multithreading.

优势

advantage

一个采用了多线程技术的应用程序可以更好地利用系统资源。其主要优势在于充分利用了 CPU 的空闲时间片,可以用尽可能少的时间来对用户的要求做出响应,使得进程的整体运行效率得到较大提高,同时增强了应用程序的灵活性。更为重要的是,由于同一进程的所有线程是共享同一内存,所以不需要特殊的数据传送机制,不需要建立共享存储区或共享文件,从而使得不同任务之间的协调操作与运行、数据的交互、资源的分配等问题更加易于解决。

An application program with multithreading technology can make better use of system resources. Its main advantage is to make full use of the free time slice of the CPU, to respond to the user’s requirements with as little time as possible, so that the overall running efficiency of the process is greatly improved, and at the same time, the flexibility of the application program is enhanced. More importantly, because all threads of the same process share the same memory, no special data transfer mechanism is needed, no shared storage area or shared file is needed, so that the coordination operation and operation between different tasks, data interaction, resource allocation and other problems are easier to solve.

线程同步

Thread synchronization

在多线程应用中,考虑不同线程之间的数据同步和防止死锁。当两个或多个线程之间同时等待对方释放资源的时候就会形成线程之间的死锁。为了防止死锁的发生,需要通过同步来实现线程安全。在 Visual Basic 中提供了三种方法来完成线程的同步。在 Java 中可用synchronized 关键字。

In multithreaded applications, data synchronization between different threads and deadlock prevention are considered. When two or more threads wait for each other to release resources at the same time, a deadlock between threads will be formed. In order to prevent deadlock, it is necessary to realize thread safety by synchronization. There are three ways to complete thread synchronization in Visual Basic. The synchronized keyword is available in Java.

代码域同步

Code field synchronization

使用 Monitor 类可以同步静态/实例化的方法的全部代码或者部分代码段。

Use the monitor class to synchronize all or part of the code snippets of static / instantiated methods.

手工同步:

Manual synchronization:

可以使用不同的同步类(诸如 WaitHandle, Mutex, ReaderWriterLock, ManualResetEvent, AutoResetEvent 和 Interlocked 等)创建自己的同步机制。这种同步方式要求你自己手动的为不同的域和方法同步,这种同步方式也可以用于进程间的同步和解除由于对共享资源的等待而造成的死锁。

You can use different synchronization classes (such as waithandle, mutex, readerwriterlock, manualresetevent, AutoResetEvent and interlocked) to create your own synchronization mechanism. This synchronization method requires you to manually synchronize for different domains and methods. This synchronization method can also be used to synchronize between processes and remove deadlock caused by waiting for shared resources.

优点

advantage

·使用线程可以把占据时间长的程序中的任务放到后台去处理。

·Threads can be used to process tasks in programs that take up a long time in the background.

·用户界面可以更加吸引人,这样比如用户点击了一个按钮去触发某些事件的处理,可以弹出一个进度条来显示处理的进度。

·The user interface can be more attractive. For example, if the user clicks a button to trigger the processing of certain events, a progress bar can pop up to display the processing progress.

·程序的运行速度可能加快。

·The program may run faster.

·在一些等待的任务实现上如用户输入、文件读写和网络收发数据等,线程就比较有用了。在这种情况下可以释放一些珍贵的资源如内存占用等等。

·In some waiting tasks, such as user input, file reading and writing, and network receiving and sending data, threads are more useful. In this case, some precious resources can be released, such as memory occupation and so on.

缺点

shortcoming

·如果有大量的线程,会影响性能,因为操作系统需要在它们之间切换。

·If there are a large number of threads, performance will be affected because the operating system needs to switch between them.

·更多的线程需要更多的内存空间。

·More threads need more memory.

·线程可能会给程序带来更多“bug”,因此要小心使用。

·Threads may cause more “bugs” to your program, so be careful.

·线程的中止需要考虑其对程序运行的影响。

·Thread termination needs to consider its impact on program operation.

·通常块模型数据是在多个线程间共享的,需要防止线程死锁情况的发生。

·Generally, block model data is shared among multiple threads, so it is necessary to prevent thread deadlock.

一些线程模型的背景

Background of some thread models

可以重点讨论一下在 Win32 环境中常用的一些模型。

You can focus on some models commonly used in Win32 environment.

·单线程模型

·Single thread model

在这种线程模型中,一个进程中只能有一个线程,剩下的进程必须等待当前的线程执行完。这种模型的缺点在于系统完成一个很小的任务都必须占用很长的时间。

In this thread model, there can only be one thread in a process, and the remaining processes must wait for the current thread to finish executing. The disadvantage of this model is that it takes a long time for the system to complete a very small task.

·块线程模型(单线程多块模型 STA)

·Block thread model (single thread multi block model STA)

这种模型里,一个程序里可能会包含多个执行的线程。在这里,每个线程被分为进程里一个单独的块。每个进程可以含有多个块,可以共享多个块中的数据。程序规定了每个块中线程的执行时间。所有的请求通过 Windows 消息队列进行串行化,这样保证了每个时刻只能访问一个块,因而只有一个单独的进程可以在某一个时刻得到执行。这种模型比单线程模型的好处在于,可以响应同一时刻的多个用户请求的任务而不只是单个用户请求。但它的性能还不是很好,因为它使用了串行化的线程模型,任务是一个接一个得到执行的。

In this model, a program may contain multiple threads of execution. Here, each thread is divided into a separate block within the process. Each process can contain multiple blocks and share data in multiple blocks. The program specifies the execution time of the threads in each block. All requests are serialized through the windows message queue, which ensures that only one block can be accessed at each time, so only a single process can be executed at a certain time. The advantage of this model over the single thread model is that it can respond to multiple user requests at the same time, rather than just a single user request. But its performance is not very good, because it uses the serial thread model, and the tasks are executed one by one.

·多线程块模型(自由线程块模型)

·Multi thread block model (free thread block model)

多线程块模型(MTA)在每个进程里只有一个块而不是多个块。这单个块控制着多个线程而不是单个线程。这里不需要消息队列,因为所有的线程都是相同的块的一个部分,并且可以共享。这样的程序比

The multithreaded block model (MTA) has only one block in each process instead of multiple blocks. This single block controls multiple threads instead of a single thread. Message queuing is not required because all threads are part of the same block and can be shared. Such program ratio


速搜资源网 , 版权所有丨如未注明 , 均为原创丨转载请注明原文链接:【速搜问答】多线程是什么
喜欢 (0)
[361009623@qq.com]
分享 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址