## Chapter 4 ### Summary - Thread is a basic unit for CPU utilization. Threads which belong to same process share many resources including code and data. - There are four main advantages for multi-threaded applications: 1. Responsiveness 2. Resource sharing 3. Economy 4. Scalability - Concurrency exists when multiple threads are making progress, and parallelism exists when multiple threads are making progress simultaneously. Parallelism requires multicore system that provides multiple CPUs. - Challenges while designing multithreaded applications: 1. Dividing and balancing the work. 2. Dividing the data between different threads. 3. identifying data dependencies 4. Test and debug. - Data parallelism distributes subsets of the same data across different computing cores and performs the same operation on each core. - Task parallelism distributes not data but tasks across multiple cores. Each task is running a unique operation. - User applications create user-level threads, which must ultimately be mapped to kernel threads to execute on a CPU. The many-to-one model maps many user-level threads to one kernel thread. Other approaches include the one-to-one and many-to-many models. - A thread library provides an API for creating and managing threads. Three common thread libraries include Windows, Pthreads, and Java threading. Windows is for the Windows system only, while Pthreads is available for POSIX-compatible systems such as UNIX, Linux, and macOS. Java threads will run on any system that supports a Java virtual machine. - Implicit threading involves identifying tasks—not threads—and allowing languages or API frameworks to create and manage threads. There are several approaches to implicit threading, including thread pools, fork-join frameworks, and Grand Central Dispatch. Implicit threading is becoming an increasingly common technique for programmers to use in developing concurrent and parallel applications. - Threads may be terminated using either asynchronous or deferred cancellation. Asynchronous cancellation stops a thread immediately, even if it is in the middle of performing an update. Deferred cancellation informs a thread that it should terminate but allows the thread to terminate in an orderly fashion. In most circumstances, deferred cancellation is preferred to asynchronous termination. - Unlike many other operating systems, Linux does not distinguish between processes and threads; instead, it refers to each as a task. The Linux clone() system call can be used to create tasks that behave either more like processes or more like threads. ### 总结 - 线程是CPU调度的基本单位,属于同一进程的线程共享该进程的许多资源,包括代码和数据。 - 多线程应用程序主要有四大好处:(1)响应性;(2)资源共享;(3)经济性;(4)可扩展性。 - 当多个线程都在推进时就存在并发,而当多个线程同时推进时则存在并行。并行需要多核系统来提供多个CPU。 - 设计多线程应用程序面临一些挑战。这些挑战包括对工作进行划分和平衡、在不同线程间分配数据,以及识别数据依赖关系。此外,多线程程序的测试和调试尤其具有挑战性。 - 数据并行将相同数据的子集分配到不同的计算核心,并在每个核心上执行相同的操作。任务并行则是将任务而非数据分配到多个核心,每个任务执行不同的操作。 - 用户应用程序创建用户级线程,而用户级线程最终必须映射到内核线程才能在CPU上执行。多对一模型将多个用户级线程映射到一个内核线程。其他模型还包括一对一和多对多模型。 - 线程库提供了创建和管理线程的API。三种常见的线程库分别是Windows线程库、Pthreads线程库和Java线程库。Windows线程库仅适用于Windows系统,而Pthreads线程库可用于与POSIX兼容的系统,如UNIX、Linux和macOS。Java线程可以在任何支持Java虚拟机的系统上运行。 - 隐式线程是指识别任务而非线程,并让语言或API框架来创建和管理线程。隐式线程有多种实现方式,包括线程池、Fork - Join框架和Grand Central Dispatch。隐式线程正日益成为程序员开发并发和并行应用程序时常用的技术。 - 线程可以使用异步取消或延迟取消来终止。异步取消会立即停止线程,即使线程正在执行更新操作。延迟取消会通知线程应该终止,但允许线程有序地终止。在大多数情况下,延迟取消比异步取消更可取。 - 与许多其他操作系统不同,Linux并不区分进程和线程,而是将它们都称为任务。Linux的`clone()`系统调用可用于创建行为更像进程或更像线程的任务。 Loading... ## Chapter 4 ### Summary - Thread is a basic unit for CPU utilization. Threads which belong to same process share many resources including code and data. - There are four main advantages for multi-threaded applications: 1. Responsiveness 2. Resource sharing 3. Economy 4. Scalability - Concurrency exists when multiple threads are making progress, and parallelism exists when multiple threads are making progress simultaneously. Parallelism requires multicore system that provides multiple CPUs. - Challenges while designing multithreaded applications: 1. Dividing and balancing the work. 2. Dividing the data between different threads. 3. identifying data dependencies 4. Test and debug. - Data parallelism distributes subsets of the same data across different computing cores and performs the same operation on each core. - Task parallelism distributes not data but tasks across multiple cores. Each task is running a unique operation. - User applications create user-level threads, which must ultimately be mapped to kernel threads to execute on a CPU. The many-to-one model maps many user-level threads to one kernel thread. Other approaches include the one-to-one and many-to-many models. - A thread library provides an API for creating and managing threads. Three common thread libraries include Windows, Pthreads, and Java threading. Windows is for the Windows system only, while Pthreads is available for POSIX-compatible systems such as UNIX, Linux, and macOS. Java threads will run on any system that supports a Java virtual machine. - Implicit threading involves identifying tasks—not threads—and allowing languages or API frameworks to create and manage threads. There are several approaches to implicit threading, including thread pools, fork-join frameworks, and Grand Central Dispatch. Implicit threading is becoming an increasingly common technique for programmers to use in developing concurrent and parallel applications. - Threads may be terminated using either asynchronous or deferred cancellation. Asynchronous cancellation stops a thread immediately, even if it is in the middle of performing an update. Deferred cancellation informs a thread that it should terminate but allows the thread to terminate in an orderly fashion. In most circumstances, deferred cancellation is preferred to asynchronous termination. - Unlike many other operating systems, Linux does not distinguish between processes and threads; instead, it refers to each as a task. The Linux clone() system call can be used to create tasks that behave either more like processes or more like threads. ### 总结 - 线程是CPU调度的基本单位,属于同一进程的线程共享该进程的许多资源,包括代码和数据。 - 多线程应用程序主要有四大好处:(1)响应性;(2)资源共享;(3)经济性;(4)可扩展性。 - 当多个线程都在推进时就存在并发,而当多个线程同时推进时则存在并行。并行需要多核系统来提供多个CPU。 - 设计多线程应用程序面临一些挑战。这些挑战包括对工作进行划分和平衡、在不同线程间分配数据,以及识别数据依赖关系。此外,多线程程序的测试和调试尤其具有挑战性。 - 数据并行将相同数据的子集分配到不同的计算核心,并在每个核心上执行相同的操作。任务并行则是将任务而非数据分配到多个核心,每个任务执行不同的操作。 - 用户应用程序创建用户级线程,而用户级线程最终必须映射到内核线程才能在CPU上执行。多对一模型将多个用户级线程映射到一个内核线程。其他模型还包括一对一和多对多模型。 - 线程库提供了创建和管理线程的API。三种常见的线程库分别是Windows线程库、Pthreads线程库和Java线程库。Windows线程库仅适用于Windows系统,而Pthreads线程库可用于与POSIX兼容的系统,如UNIX、Linux和macOS。Java线程可以在任何支持Java虚拟机的系统上运行。 - 隐式线程是指识别任务而非线程,并让语言或API框架来创建和管理线程。隐式线程有多种实现方式,包括线程池、Fork - Join框架和Grand Central Dispatch。隐式线程正日益成为程序员开发并发和并行应用程序时常用的技术。 - 线程可以使用异步取消或延迟取消来终止。异步取消会立即停止线程,即使线程正在执行更新操作。延迟取消会通知线程应该终止,但允许线程有序地终止。在大多数情况下,延迟取消比异步取消更可取。 - 与许多其他操作系统不同,Linux并不区分进程和线程,而是将它们都称为任务。Linux的`clone()`系统调用可用于创建行为更像进程或更像线程的任务。 最后修改:2025 年 03 月 23 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果觉得我的文章对你有用,请随意赞赏