术语“队列"、“多核"和“线程"的使用在大中央调度 [英] Use of the terms "queues", "multicore", and "threads" in Grand Central Dispatch

查看:27
本文介绍了术语“队列"、“多核"和“线程"的使用在大中央调度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力理解 Grand Central Dispatch 的概念.我想从 Vandad 的并发编程一书中理解这些引语.

I am trying to get my head around the concepts of Grand Central Dispatch. I want to understand these quotes from Vandad's book on Concurrent Programming.

GCD 的真正用途是将任务分派到多个内核,而不必让程序员担心哪个内核正在执行哪个任务.

The real use for GCD is to dispatch tasks to multiple cores, without making you the programmer, worry about which core is executing which task.

GCD 的核心是调度队列.调度队列是线程池.

At the heart of GCD are dispatch queues. Dispatch queues are pools of threads.

最后

您不会直接使用这些线程.您将只使用调度队列,将任务调度到这些队列并要求队列调用您的任务.

You will not be working with these threads directly. You will just work with dispatch queues, dispatching tasks to these queues and asking queues to invoke your task.

我已将关键术语加粗.

多核与队列相同吗?队列是否由多个线程组成?每个线程都执行一项任务吗?

Are multiple cores the same as queues? Does a queue consist of many threads? Does each thread perform a task?

推荐答案

那么多核和队列是一样的吗?

So multiple cores are the same as queues?

不是真的.队列是一种编程抽象,核心是处理器中的物理资源.队列和核心之间没有唯一的关系,尽管在任何给定时间点都可以说给定队列正在给定核心上执行给定任务.

Not really. A queue is a programming abstraction, a core is a physical resource in your processor. There is no unique relationship between a queue and a core, although at any given point in time it can be said that a given queue is executing a given task on a given core.

一个队列由许多线程组成?

A queue consists of many threads?

队列由任务组成.当需要执行该任务时,任务由队列管理系统分配给线程.线程是操作系统资源,分配给内核,内核有效地运行它们,并且不知道任务是什么(超线程 CPU 除外).

A queue consists of tasks. Tasks are assigned to threads by the queue managing system when it comes the time to execute that task. Threads are OS resources and are allocated to cores, which effectively run them and have no notion of what a task is (except for Hyper-Threading CPUs).

如果您不考虑硬件多线程(例如,超线程),那么在任何给定时间点,核心都在运行特定线程;当需要运行不同的线程时,该内核中会发生上下文切换.如果考虑到硬件多线程,则可以在同一物理内核中托管的虚拟内核上运行多个线程.

If you do not account for hardware-multithreading (e.g., Hyper-threading), at any given point in time a core is running a specific thread; when it comes the time to run a different thread, a context-switch occurs in that core. If you account for hardware-multithreading, you can have multiple threads running on virtual cores hosted in the same physical core.

队列和线程之间的关系是不透明的.一个队列可以同时管理多个线程,或者一次管理多个线程,或者一直只管理一个;在第一种情况下,您有一个并行队列,能够在并发线程上执行并行任务;在第二种和第三种情况下,你有一个串行队列.

The relationship between queues and threads is opaque. A queue could manage several threads at once, or several threads once at a time, or just one all the time; in the first case, you have a parallel queue, able to execute parallel tasks on simultaneous threads; in the second and third case, you have a serial queue.

每个线程执行一个任务?

Each thread performs a task?

在任何给定的时间点,一个线程正在执行一项任务.你可以让线程产生,执行它们的任务,然后死掉;或者您可以拥有执行多个任务的长时间运行的线程(即主线程).

At any given point in time, a thread is performing a task. You can have threads that are spawn, execute their task, and die; or you can have long running threads (i.e., the main thread) that execute several tasks.

也许一开始就很令人费解,您可能需要阅读一些有关操作系统和高级处理器架构的知识才能完全理解这一点.

Maybe it is pretty puzzling at start, you might need some reading about Operating Systems and maybe high-level Processor Architectures to fully understand this.

GCD 旨在让您仅以抽象术语进行推理:即,就任务和队列而言,而忘记线程和内核,它们被视为一种实现手段",或者您可以使用的低级细节留给系统高效使用.

GCD aims at letting you reason exclusively in abstract terms: i.e., in terms of tasks and queues, and forget about threads and cores, that are seen as a sort of "implementation means", or low-level details that you can leave to the system to use efficiently.

这篇关于术语“队列"、“多核"和“线程"的使用在大中央调度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆