Dispatch Queues

Dispatch queues are a C-based mechanism for executing custom tasks. A dispatch queue executes tasks either serially or concurrently but always in a first-in, first-out order. (In other words, a dispatch queue always dequeues and starts tasks in the same order in which they were added to the queue.) A serial dispatch queue runs only one task at a time, waiting until that task is complete before dequeuing and starting a new one. By contrast, a concurrent dispatch queue starts as many tasks as it can without waiting for already started tasks to finish.

Dispatch queues have other benefits:

  • They provide a straightforward and simple programming interface.

  • They offer automatic and holistic thread pool management.

  • They provide the speed of tuned assembly.

  • They are much more memory efficient (because thread stacks do not linger in application memory).

  • They do not trap to the kernel under load.

  • The asynchronous dispatching of tasks to a dispatch queue cannot deadlock the queue.

  • They scale gracefully under contention.

  • Serial dispatch queues offer a more efficient alternative to locks and other synchronization primitives.

The tasks you submit to a dispatch queue must be encapsulated inside either a function or a block object. Block objects are a C language feature introduced in OS X v10.6 and iOS 4.0 that are similar to function pointers conceptually, but have some additional benefits. Instead of defining blocks in their own lexical scope, you typically define blocks inside another function or method so that they can access other variables from that function or method. Blocks can also be moved out of their original scope and copied onto the heap, which is what happens when you submit them to a dispatch queue. All of these semantics make it possible to implement very dynamic tasks with relatively little code.

Dispatch queues are part of the Grand Central Dispatch technology and are part of the C runtime. For more information about using dispatch queues in your applications, see Dispatch Queues. For more information about blocks and their benefits, see Blocks Programming Topics.

https://developer.apple.com/library/content/documentation/General/Conceptual/ConcurrencyProgrammingGuide/ConcurrencyandApplicationDesign/ConcurrencyandApplicationDesign.html#//apple_ref/doc/uid/TP40008091-CH100-SW2

Dispatch Queues 线程池的更多相关文章

  1. 线程池ThreadPool及Task调度死锁分析

    近1年,偶尔发生应用系统启动时某些操作超时的问题,特别在使用4核心Surface以后.笔记本和台式机比较少遇到,服务器则基本上没有遇到过. 这些年,我写的应用都有一个习惯,就是启动时异步做很多准备工作 ...

  2. .net线程池内幕

    本文通过对.NET4.5的ThreadPool源码的分析讲解揭示.NET线程池的内幕,并总结ThreadPool设计的好与不足. 线程池的作用线程池,顾名思义,线程对象池.Task和TPL都有用到线程 ...

  3. python线程池实现

    python 的线程池主要有threadpool,不过它并不是内置的库,每次使用都需要安装,而且使用起来也不是那么好用,所以自己写了一个线程池实现,每次需要使用直接import即可.其中还可以根据传入 ...

  4. java多线程系类:JUC线程池:03之线程池原理(二)(转)

    概要 在前面一章"Java多线程系列--"JUC线程池"02之 线程池原理(一)"中介绍了线程池的数据结构,本章会通过分析线程池的源码,对线程池进行说明.内容包 ...

  5. .net线程池

    线程池的作用线程池,顾名思义,线程对象池.Task和TPL都有用到线程池,所以了解线程池的内幕有助于你写出更好的程序.由于篇幅有限,在这里我只讲解以下核心概念: 线程池的大小 如何调用线程池添加任务 ...

  6. python——有一种线程池叫做自己写的线程池

    这周的作业是写一个线程池,python的线程一直被称为鸡肋,所以它也没有亲生的线程池,但是竟然被我发现了野生的线程池,简直不能更幸运~~~于是,我开始啃源码,实在是虐心,在啃源码的过程中,我简略的了解 ...

  7. Java多线程系列--“JUC线程池”03之 线程池原理(二)

    概要 在前面一章"Java多线程系列--“JUC线程池”02之 线程池原理(一)"中介绍了线程池的数据结构,本章会通过分析线程池的源码,对线程池进行说明.内容包括:线程池示例参考代 ...

  8. ACE - ACE_Task源码剖析及线程池实现

    原文出自http://www.cnblogs.com/binchen-china,禁止转载. 上篇提到用Reactor模式,利用I/O复用,获得Socket数据并且实现I/O层单线程并发,和dispa ...

  9. Android线程和线程池

    Translated From Google Android. class PhotoDecodeRunnable implements Runnable {...    /*     * Defin ...

随机推荐

  1. Quartz.Net 使用心得(二)

    工作中需要做一个简易的Cron字符串生成器,并且要获取生成的Cron后面10次的触发时间来验证. 此问题困扰了我很久时间,CSDN上有一个Java版本的,本人菜鸟,想移植到C#中,语法上怎么也不通过. ...

  2. Oracle Multitenant Environment (二) Purpose

    Purpose of a Multitenant Environment A multitenant environment enables the central management of mul ...

  3. python列表可以加可以乘

    python列表可以加可以乘 list=['abcd',786,2.23,'runoob',70.2] tinylist = [123,'runoob'] print(list) print(list ...

  4. Android:Unable to resolve target ‘android-8’问题解决

    在学习过程中,下载别人写过的代码,导入到eclipse,报错: Unable to resolve target 'android-8' 问题是项目版本号问题. 解决方法例如以下: 右击项目:Prop ...

  5. 可恢复的安全rm

    我们常常使用rm去删除一些文件.假设不小手一抖,那么就悲剧了.你们都懂的... 在经历过一次这种慘剧后.决定永远杜绝这种情况.重写写了shell函数.运行安全的rm.这个函数会把要删除的文件按日期备份 ...

  6. CocoaPods 的安装和使用介绍

    CocoaPods 的安装和使用介绍 安装 安装方式异常简单 , Mac 下都自带 ruby,使用 ruby 的 gem 命令即可下载安装: 1 2 $ sudo gem install cocoap ...

  7. SVN提交代码冲突解决方法总结

    在近期svn提交代码时遇到多种情况的问题,现做一个解决办法的总结如下: 插播一条:idea下被svn版本控制各类文件的颜色区分,红棕色,未加入版本控制:绿色,已经加入控制暂未提交:蓝色,加入,已提交, ...

  8. torch lua tips

    1.luarocks 离线安装Moses 下载 Moses-Moses-1.6.1-1.zip cd Moses-Moses-1.6.1-1 Moses-Moses-1.6.1-1# luarocks ...

  9. android:descendantFocusability

    开发中很常见的一个问题,项目中的GridView不仅仅是简单的文字和图片,常常需要自己定义GridView,自己的Adapter去继承BaseAdapter,在adapter中按照需求进行编写,问题就 ...

  10. git的基本操作流程

    1.git clone 初始会有默认的master分支,并且master和origin/master自动建立了映射关系 2. git checkout -b local    创建并且切换到local ...