Dispatch Queues and Thread Safety

It might seem odd to talk about thread safety in the context of dispatch queues, but thread safety is still a relevant topic. Any time you are implementing concurrency in your application, there are a few things you should know:

  • Dispatch queues themselves are thread safe. In other words, you can submit tasks to a dispatch queue from any thread on the system without first taking a lock or synchronizing access to the queue.

  • Do not call the dispatch_sync function from a task that is executing on the same queue that you pass to your function call. Doing so will deadlock the queue. If you need to dispatch to the current queue, do so asynchronously using the dispatch_async function.

  • Avoid taking locks from the tasks you submit to a dispatch queue. Although it is safe to use locks from your tasks, when you acquire the lock, you risk blocking a serial queue entirely if that lock is unavailable. Similarly, for concurrent queues, waiting on a lock might prevent other tasks from executing instead. If you need to synchronize parts of your code, use a serial dispatch queue instead of a lock.

  • Although you can obtain information about the underlying thread running a task, it is better to avoid doing so. For more information about the compatibility of dispatch queues with threads, see Compatibility with POSIX Threads.

https://developer.apple.com/library/archive/documentation/General/Conceptual/ConcurrencyProgrammingGuide/OperationQueues/OperationQueues.html#//apple_ref/doc/uid/TP40008091-CH102-SW28

Dispatch Queues and Thread Safety的更多相关文章

  1. iOS 并行编程:GCD Dispatch Queues

    1 简介 1.1 功能          Grand Central Dispatch(GCD)技术让任务并行排队执行,根据可用的处理资源,安排他们在任何可用的处理器核心上执行任务.任务可以是一个函数 ...

  2. 线程安全 Thread Safety Problem scala concurrency 并发

    小结: 1.基于java并发模型 Scala concurrency is built on top of the Java concurrency model. 2. 将每个请求放入一个新的线程 T ...

  3. Replacing Threads with Dispatch Queues

    Replacing Threads with Dispatch Queues To understand how you might replace threads with dispatch que ...

  4. Dispatch Queues 线程池

    Dispatch Queues Dispatch queues are a C-based mechanism for executing custom tasks. A dispatch queue ...

  5. Thread Safety线程安全

    Thread Safe(线程安全)和None Thread Safe(NTS,非线程安全)之分 如果disabled就选择nts(php_stomp-1.0.9-5.5-nts-vc11-x86.zi ...

  6. Effective Java 70 Document thread safety

    Principle The presence of the synchronized modifier in a method declaration is an implementation det ...

  7. Java Concurrency In Practice -Chapter 2 Thread Safety

    Writing thread-safe code is managing access to state and in particular to shared, mutable state. Obj ...

  8. Thread safety

    https://en.wikipedia.org/wiki/Thread_safety Thread safety is a computer programming concept applicab ...

  9. 折返(Reentrancy)VS线程安全(Thread safety)

    在Wiki上,折返例如,下面的定义(接) In computing, a computer program or subroutine is called reentrant if it can be ...

随机推荐

  1. Sum of Medians

    Sum of Medians time limit per test 3 seconds memory limit per test 256 megabytes In one well-known a ...

  2. Linux中安装MongoDB出现的问题记录

    mongoDB安装完成后,运行sudo service mongod start 查看程序状态:ps ajx | grep mongod   ,启动失败 查看失败信息提示,终端命令:tail -f / ...

  3. 调度器Quartz的配置文件中的线程池设置

    在使用调度器Quartz来进行数据归档的时候,当我们开的定时任务很多的时候,就会出现一些定时任务不会被触发的现象,这就是线程阻塞.那到底什么叫线程阻塞呢? 线程阻塞,顾名思义就是说线程被阻塞了,没有按 ...

  4. springboot之多任务并行+线程池处理

    最近项目中做到一个关于批量发短信的业务,如果用户量特别大的话,不能使用单线程去发短信,只能尝试着使用多任务来完成!我们的项目使用到了方式二,即Future的方案 Java 线程池 Java通过Exec ...

  5. [Bzoj3940] [AC自动机,USACO 2015 February Gold] Censor [AC自动机模板题]

    AC自动机模板题(膜jcvb代码) #include <iostream> #include <algorithm> #include <cstdio> #incl ...

  6. HTML-class与id的区别及应用

    在样式表定义一个样式的时候,可以定义id也可以定义class. 1.在CSS文件里书写时,ID加前缀"#":CLASS用"." 如只能用id #nav { wi ...

  7. [Cypress] Test Variations of a Feature in Cypress with a data-driven Test

    Many applications have features that can be used with slight variations. Instead of maintaining mult ...

  8. 我的Android进阶之旅------&gt;Android中ListView中嵌套(ListView)控件时item的点击事件不起作的问题解决方法

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb3V5YW5nX3Blbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  9. VassistX 凝视 模板

    避免头文件反复包括宏定义: #ifndef $FILE_BASE_UPPER$_H_ #define $FILE_BASE_UPPER$_H_ $selected$ #endif // $FILE_B ...

  10. 浅谈PHP数据结构之单链表

    什么是链表?(依据百度词条查询而得) 链表是一种物理存储单元上非连续.非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的.链表由一系列结点(链表中每个元素称为结点)组成,结点能够在执 ...