Dispatch Queues and Thread Safety

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

原文地址:https://www.cnblogs.com/feng9exe/p/9174407.html

时间: 2024-11-13 08:00:22

Dispatch Queues and Thread Safety的相关文章

Dispatch Queues调度队列

前言-死锁案例 // 在主线程中执行 dispatch_queue_t queueMain = dispatch_get_main_queue(); dispatch_sync(queueMain, ^{ NSLog(@"+++++++"); }); NSLog(@"hahahaha"); 案例分析:运行结果是程序阻塞在dispatch_sync()处.由于main线程执行到dispatch_sync()处,线程处于等待状态.将block任务块添加到主串行队列最后,

iOS 并行编程:GCD Dispatch Queues

1 简介 1.1 功能       Grand Central Dispatch(GCD)技术让任务并行排队执行,根据可用的处理资源,安排他们在任何可用的处理器核心上执行任务.任务可以是一个函数(function)或者是一个block. GCD的底层依然是用线程实现,不过这样可以让程序员不用关注实现的细节. GCD中的队列称为dispatch queue,它可以保证先进来的任务先得到执行通过它能够大大简化多线程编程.工程师只要将要执行的任务(执行代码块)放入队列中,GCD将会为需要执行的任务创建

Dispatch Queues

GCD(Grand Central Dispatch)的 dispatch queues 是一个实现多任务的很好的工具.Dispatch queues 让你能够方便的使用blocks,不管你想要去调用同步或异步.你可以实现几乎所有的以前你通过separate 的threads完成的任务.相对thread的code 而言,dispatch queues的优点是更简单和更有效率. 这章节提供了dispatch queues的详细介绍,所有关于怎么样使用他们去执行普通任务的信息. 一.关于Dispat

GCD介绍(一):基本概念和Dispatch Queues

什么是GCD? Grand Central Dispatch或者GCD,是一套低层API,提供了一种新的方法来进行并发程序编写.从基本功能上讲,GCD有点像NSOperationQueue,他们都允许程序将任务切分为多个单一任务然后提交至工作队列来并发地或者串行地执行.GCD比之NSOpertionQueue更底层更高效,并且它不是Cocoa框架的一部分. 除了代码的平行执行能力,GCD还提供高度集成的事件控制系统.可以设置句柄来响应文件描述符.mach ports(Mach port 用于 O

Dispatch Queues 线程池

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 start

详解IOS开发应用之并发Dispatch Queues (2011)

详解IOS开发应用之并发Dispatch Queues是本文哟啊介绍的内容,我们几乎可以调度队列去完成所有用线程来完成的任务.调度队列相对于线程代码更简单,易于使用,更高效.下面讲主要简述调度队列,在应用中如何使用调度队列去执行任务. 1.关于调度队列 所有的调度队列都是先进先出队列,因此,队列中的任务的开始的顺序和添加到队列中的顺序相同.GCD自动的为我们提供了一些调度队列,我们也可以创建新的用于具体的目的. 下面列出几种可用的调度队列类型以及如何使用. (1)serial queues(串行

dispatch queues GCD

我们几乎可以调度队列去完成所有用线程来完成的任务.调度队列相对于线程代码更简单,易于使用,更高效. 下面讲主要简述调度队列,在应用中如何使用调度队列去执行任务. 1.关于调度队列 所有的调度队列都是先进先出队列,因此,队列中的任务的开始的顺序和添加到队列中的顺序相同.GCD自动的为我们提供了一些调度队列,我们也可以创建新的用于具体的目的. 下面列出几种可用的调度队列类型以及如何使用. (1)serial queues(串行队列)又称私有调度队列(private),一般用在对特定资源的同步访问上.

Java Concurrency In Practice -Chapter 2 Thread Safety

Writing thread-safe code is managing access to state and in particular to shared, mutable state. Object's state is its data, stored in state variables such as instance or static fields. Whether an object needs to be thread-safe depends on whether it

Effective Java 70 Document thread safety

Principle The presence of the synchronized modifier in a method declaration is an implementation detail, not a part of its exported API. To enable safe concurrent use, a class must clearly document what level of thread safety it supports. Immutable ?