转自:http://blog.sina.com.cn/s/blog_74bf41030101225d.html
GCD 概述
原文:http://janedoneway.iteye.com/blog/1403299
1. GCD 包含于 libSystem.dylib
2. 可供所有程序使用.
- #include <dispatch/dispatch.h>
3. GCD API 提供 block-based and function-based variants
- 目前仅提供 block-based API
GCD总结
1. Blocks
- dispatch_async()
2. Queues
- Lightweight list of blocks
- Enqueue/dequeue is FIFO
3. dispatch_get_main_queue()
- Main thread/main runloop
4. dispatch_queue_create()
- Automatic helper thread
GCD的好处
1. 高效 - More CPU cycles available for your code
2. 使用方便
- Blocks are easy to use
- Queues are inherently producer/consumer
3. Systemwide perspective
- Only the OS can balance unrelated subsystems
兼容性
1. Existing threading and synchronization primitives are 100% compatible
2. GCD threads are wrapped POSIX threads
- Do not cancel, exit, kill, join, or detach GCD threads
3. GCD reuses threads
- Restore any per-thread state changed within a block
多线程
Session 206已提到此内容。
锁定资源
1. 对关键资源进行互斥访问。
2. 在线程中按序访问共享资源。
3. Ensure data integrity
没有GCD的代码
- (void)updateImageCacheWithImg:(UIImage*)img {
NSLock *l = self.imageCacheLock;
[l lock];
// Critical section
if ([self.imageCache containsObj:img]) {
[l unlock]; // Don't forget to unlock
return;
}
[self.imageCache addObj:img];
[l unlock];
}
GCD代码
- (void)updateImageCacheWithImg:(NSImage*)img {
dispatch_queue_t queue = self.imageCacheQueue;
dispatch_sync(queue, ^{ //You can change dispatch_sync to dispatch_async to defer critical section
// Critical section
if ([self.imageCache containsObj:img]) {
return;
}
[self.imageCache addObj:img];
});
}
线程间通信
通过以下方法 (Without GCD)
– performSelectorOnMainThread:withObject:waitUntilDone:
– performSelector:onThread:withObject:waitUntilDone:
– performSelector:withObject:afterDelay:
– performSelectorInBackground:withObject:
GCD 代码,等同于 performSelector:onThread:withObject:waitUntilDone:
// waitUntilDone: NO
dispatch_async(queue, ^{
[myObject doSomething:foo withData:bar];
});
// waitUntilDone: YES
dispatch_sync(queue, ^{
[myObject doSomething:foo withData:bar];
});
GCD代码, 等同于 performSelector:withObject:afterDelay:dispatch_time_t delay;delay = dispatch_time(DISPATCH_TIME_NOW, 50000 );dispatch_after(delay, queue, ^{ [myObject doSomething:foo withData:bar];});GCD代码,等同于 performSelectorInBackground:withObject:dispatch_queue_t queue = dispatch_get_global_queue(0, 0);dispatch_async(queue, ^{ [myObject doSomething:foo withData:bar];});
Global Queues1. Enqueue/dequeue is FIFO
2. Concurrent execution- Non-FIFO completion order
3. dispatch_get_global_queue(priority, 0)
4. Global queues map GCD activity to real threads 5. Priority bands- DISPATCH_QUEUE_PRIORITY_HIGH - DISPATCH_QUEUE_PRIORITY_DEFAULT - DISPATCH_QUEUE_PRIORITY_LOW
下面三节讲述了很多内容,但我还没有理解。如有兴趣,请看原始视频。- Dispatch Sources
- Source Cancellation- Target Queues
GCD对象
GCD方法大全
管理对象的生命周期
1. Ensure objects captured by blocks are valid when blocks are executed
- Objects must be retained and released around asynchronous operations
2. Objective-C objects captured by blocks are auto-retained and auto-released
3. Other objects captured by blocks must be retained by your code
- CFRetain()/CFRelease()
- dispatch_retain()/dispatch_release()
挂起和恢复
1. Suspend and resume only affects queues and sources you create
- Sources are created suspended
2. Suspension is asynchronous
- Takes effect between blocks
3. Your queues can predictably suspend objects that target them
程序上下文
1. Applications can attach custom data to GCD objects
- dispatch_set_context()/dispatch_get_context()
2. Optional finalizer callback
- dispatch_set_finalizer_f()
- Allows attached context to be freed with object
- Called on the target queue of the object
WWDC2010 Session211 Simplifying iPhone App Development with Grand Central Dispatch
Daniel Steffen - Core OS
相关推荐
pytorch-grad-cam源代码阅读和调试
grad-cam Pytorch实现,类激活图,可视化feature map,热图显示图像分类的分类原因,因为什么分的类。
Grad-CAM(Gradient-weighted Class Activation Mapping)是一种可视化网络中哪些区域对分类结果有更大的贡献的方法,它能够在图像分类任务中帮助我们理解神经网络是如何作出决策的。Grad-CAM 通过利用深度卷积神经...
刘彬20000词汇思维导图
我们调查了可解释性问题 视觉对象检测器。...YOLO对象检测器的示例 如何将GradCAM集成到模型架构中并分析结果。 我们将展示如何计算基于归因的解释 个别检测并发现 结果对它们的解释有很大的影响。
Grad-CAM-张量流 注意:CNN还有另一种很棒的可视化方法,称为 ,它仅涉及前向通过。 演示代码可用于Caffe和Tensorflow ResNet,Vgg。 请检查一下。 这是用于Grad-CAM的演示的tensorflow版本。 我使用ResNet-v1-101...
pytorch实现Grad-CAM和Grad-CAM++,可以可视化任意分类网络的Class Activation Map (CAM)图,包括自定义的网络;同时也实现了目标检测faster r-cnn和retinanet两个网络的CAM图; 使用方法 python main.py --image-path ...
纯代码布局,MVC设计模式。数据神马的,自己去搞吧。我写了一个本地的plist文件,运行即可看出数据结构。都是按照接口的样式写的,如果需求也是这样的小伙伴,加上数据解析,代码直接就可以用!...
optical lithography pst-grad,optical lithography pst-grad
在Pytorch中实现Grad-CAM 是什么使网络认为图像标签是“ pug,pug-dog”和“ tabby,虎斑猫”: 将Grad-CAM与引导反向传播相结合的“ pug,pug-dog”类别: 梯度类激活图是用于深度学习网络的可视化技术。 参见...
Swin-Transformer+gradcam可视化代码+Windows环境
pytorch-Grad-CAM-热度图制作掩膜-对原图进行剪裁
Grad-CAM.pytorch pytorch 实现 和 3.1 3.3 5.1 5.2 5.3 5.4 6.1 6.2 6.3 6.4 7.1 7.2 7.3 7.4 Grad-CAM整体架构 Grad-CAM++与Grad-CAM的异同 依赖 python 3.6.x pytoch 1.0.1+ torchvision 0.2.2 ...
Grad-CAM的Pytorch实现。Pytorch implementation of Grad-CAM
它允许使用多种方法(例如,反向引导传播,Grad-Cam,Guide Grad-Cam和Grad-Cam ++)生成注意力图。 您需要添加到项目中的只是一行代码: model = gcam . inject ( model , output_dir = "attention_maps" , save_...
在Keras中实现Grad-CAM 梯度类激活图是用于深度学习网络的可视化技术。 参见论文: : 该论文的作者实现了火炬实施: : 该代码假定Tensorflow尺寸顺序,并默认在keras.applications中使用VGG16网络(网络权重将在...
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
Grad-CAM的Pytorch实现。Pytorch implementation of Grad-CAM
Pytorch中的Grad-CAM实现使网络认为图像标签是“ pug,pug-dog”和“ tabby,虎斑猫”的原因:针对“ pug,pug-dog”类将Grad-CAM与引导反向传播结合起来:Gra Grad -Pytorch中的-CAM实施使网络认为图像标签是“ pug...
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装