What Are Threads?

What Are Threads?

什么是线程?

Threads are a relatively lightweight way to implementmultiple paths of execution inside of an application.

线程是一种相对轻量级的方式用于实现应用程序内的多个执行路径(任务)。


At the system level, programs run side by side, with thesystem doling out execution time to each program based on its needs and theneeds of other programs.

从操作系统的层面看,每个应用程序请求时间,操作系统根据应用程序的请求分配时间,应用程序利用这些时间资源(操作系统分配)执行自身的任务。


Inside each program, however, exists one or more threads ofexecution, which can be used to perform different tasks simultaneously or in anearly simultaneous manner.

在每一个程序内部存在一个或多个运行中线程,可用于执行不同的任务,而任务的执行是同时或几乎同时发生的。


The system itself actually manages these threads ofexecution, scheduling them to run on the available cores and preemptivelyinterrupting them as needed to allow other threads to run.

事实上,这些线程的执行由操作系统自身管理,调度,并中断某线程用以运行其他线程。


From a technical standpoint, a thread is a combination ofthe kernel-level and application-level data structures needed to manage theexecution of code.

从技术角度来看,一根儿线程 是 两个层级(内核层级和应用程序层级)的数据结构组合,用于管理代码的执行。


The kernel-level structures coordinate the dispatching ofevents to the thread and the preemptive scheduling of the thread on one of theavailable cores.

内核层级的数据结构负责 在一个可用核心 协调 事件的派遣 和 线程抢占式调度。


The application-level structures include the call stack forstoring function calls and the structures the application needs to manage andmanipulate the thread’s attributes and state.

应用程序层级的数据结构 包括用于存储函数调用的调用堆栈和应用程序用于管理和操作的线程的属性和状态所需的结构。


In a nonconcurrent application, there is only one thread ofexecution.

在非并发的应用程序,只有一根儿执行线程。


That thread starts and ends with your application’s mainroutine and branches one-by-one to different methods or functions to implementthe application’s overall behavior.

这根儿线程随着主程序的启动而启动、随着主程序的结束而结束,并顺序的执行分支,以不同的方法或函数来实现应用程序的整体行为。


By contrast, an application that supports concurrency startswith one thread and adds more as needed to create additional execution paths.

相比之下,支持并发的应用程序以一根儿线程(主线程)启动,并创建更多线程用以创建更多的执行路径。


Each new path has its own custom start routine that runsindependently of the code in the application’s main routine.

每一个新的执行路径有其自己的自定义启动程序,而这个“自定义启动程序”是独立于主线程的。


Having multiple threads in an application provides two veryimportant potential advantages:

在应用程序中应用多线程技术带来了两个非常重要的潜在好处:


Multiple threads can improve an application’s perceivedresponsiveness.

多线程可以提高应用程序的响应。

Multiple threads can improve an application’s real-timeperformance on multicore systems.

在多核系统中,多线程可以提高应用程序的的实时性能。


If your application has only one thread, that one threadmust do everything.

如果我们的应用程序只有一根儿线程,这根儿线程必须做的所有事情。


It must respond to events, update your application’swindows, and perform all of the computations needed to implement yourapplication’s behavior.

事件响应的活,需要它去做;更新应用程序窗体的活,需要它去做;用于实现应用程序行为所需要的计算工作,需要它去做。


The problem with having just one thread is that it can onlydo one thing at a time.

单线程的问题是,它一次只能做一件事。


So what happens when one of your computations takes a longtime to finish?

那么,当我们的计算任务需要很长时间来完成,会发生什么事情?


While your code is busy computing the values it needs, yourapplication stops responding to user events and updating its windows.

当我们的代码正在忙于计算某个值,我们的应用程序就会停止响应用户事件,停止更新窗体。


If this behavior continues long enough, a user might thinkyour application is hung and try to forcibly quit it.

如果这种现象(应用程序无响应)持续足够长的时间,用户可能会认为应用程序被挂起,并试图强行退出。


If you moved your custom computations onto a separatethread, however, your application’s main thread would be free to respond touser interactions in a more timely manner.

如果我们将自定义计算放到一个单独的线程,我们的应用程序的主线程将有空去更及时地响应用户交互。


With multicore computers common these days, threads providea way to increase performance in some types of applications.

现如今,随着多核计算机的普及,线程技术提供了一种途径,用以提高某些类型的应用程序的性能。


Threads that perform different tasks can do sosimultaneously on different processor cores, making it possible for anapplication to increase the amount of work it does in a given amount of time.

多根儿线程 利用 不同的cpu 内核 可以 同时 执行不同的任务,使如下诉求成为可能:


Of course, threads are not a panacea for fixing anapplication’s performance problems.

当然,线程并不是包治百病的灵丹妙药,并非所有的应用程序的性能问题都可以用它来解决。


Along with the benefits offered by threads come thepotential problems.

伴随着线程所带来的好处,潜在问题也随之而来。


Having multiple paths of execution in an application can adda considerable amount of complexity to your code.

在应用程序中使用多线程技术会增加代码的复杂度,并且,这种增长是很可观的。


Each thread has to coordinate its actions with other threadsto prevent it from corrupting the application’s state information.

每根儿线程与其他线程 相互协调行为,以避免破坏应用程序的状态信息。


Because threads in a single application share the samememory space, they have access to all of the same data structures.

这是因为,在一个单一的应用的线程中,多根儿线程共享同一块的内存空间,他们访问相同的数据结构。


If two threads try to manipulate the same data structure atthe same time, one thread might overwrite another’s changes in a way thatcorrupts the resulting data structure.

如果两根儿线程试图同时操作相同的数据结构,一根儿线程可能会覆盖另一根儿线程的操作,从而破坏最终的数据结构。


Even with proper protections in place, you still have towatch out for compiler optimizations that introduce subtle (and not so subtle)bugs into your code.

即使有适当的保护,我们仍然要注意对编译器的优化,因为这会在代码中产生bug。


阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭