Concurrent vs serial queues in GCD
来源:互联网 发布:matlab最优化计算 编辑:程序博客网 时间:2024/05/22 11:52
转自出处:
http://stackoverflow.com/questions/19179358/concurrent-vs-serial-queues-in-gcd
I'm struggling to fully understand the concurrent and serial queues in GCD. I have some issues and hoping someone can answer me clearly and at the point.
I'm reading that serial queues are created and used in order to execute tasks one after the other. However, what happens if:
- I create a serial queue
- I use
dispatch_async
(on the serial queue I just created) three times to dispatch three blocks A,B,C
Will the three blocks be executed:
in order A,B,C because the queue is serial
OR
- concurrently (in the same time on parralel threads) because I used ASYNC dispatch
I'm reading that I can use
dispatch_sync
on concurrent queues in order to execute blocks one after the other. In that case, WHY do serial queues even exist, since I can always use a concurrent queue where I can dispatch SYNCHRONOUSLY as many blocks as I want?Thanks for any good explanation!
1 Answer
A simple example: you have a block that takes a minute to execute. You add it to a queue from the main thread. Let's look at the four cases.
- async - concurrent: the code runs on a background thread. Control returns immediately to the main thread (and UI). The block can't assume that it's the only block running on that queue
- async - serial: the code runs on a background thread. Control returns immediately to the main thread. The block can assume that it's the only block running on that queue
- sync - concurrent: the code runs on a background thread but the main thread waits for it to finish, blocking any updates to the UI. The block can't assume that it's the only block running on that queue (I could have added another block using async a few seconds previously)
- sync - serial: the code runs on a background thread but the main thread waits for it to finish, blocking any updates to the UI. The block can assume that it's the only block running on that queue
Obviously you would used either of the last two for long running processes. You normally see it when you're trying to update the UI (always on the main thread) from something that may be running on another thread.
I'm struggling to fully understand the concurrent and serial queues in GCD. I have some issues and hoping someone can answer me clearly and at the point.
I'm reading that serial queues are created and used in order to execute tasks one after the other. However, what happens if:
- I create a serial queue
- I use
dispatch_async
(on the serial queue I just created) three times to dispatch three blocks A,B,C
Will the three blocks be executed:
in order A,B,C because the queue is serial
OR
- concurrently (in the same time on parralel threads) because I used ASYNC dispatch
I'm reading that I can use
dispatch_sync
on concurrent queues in order to execute blocks one after the other. In that case, WHY do serial queues even exist, since I can always use a concurrent queue where I can dispatch SYNCHRONOUSLY as many blocks as I want?Thanks for any good explanation!
1 Answer
A simple example: you have a block that takes a minute to execute. You add it to a queue from the main thread. Let's look at the four cases.
- async - concurrent: the code runs on a background thread. Control returns immediately to the main thread (and UI). The block can't assume that it's the only block running on that queue
- async - serial: the code runs on a background thread. Control returns immediately to the main thread. The block can assume that it's the only block running on that queue
- sync - concurrent: the code runs on a background thread but the main thread waits for it to finish, blocking any updates to the UI. The block can't assume that it's the only block running on that queue (I could have added another block using async a few seconds previously)
- sync - serial: the code runs on a background thread but the main thread waits for it to finish, blocking any updates to the UI. The block can assume that it's the only block running on that queue
Obviously you would used either of the last two for long running processes. You normally see it when you're trying to update the UI (always on the main thread) from something that may be running on another thread.
- Concurrent vs serial queues in GCD
- Garbage Collection: Serial vs. Parallel vs. Concurrent-Mark-Sweep
- GCD--Serial(串行)和Concurrent(并发)、Synchronous(同步)和Asynchronous(异步)
- dispatch queues GCD
- GCD: Obtaining Dispatch Queues
- NSOperation concurrent vs non-concurrent
- Garbage Collectors – Serial vs. Parallel vs. CMS vs. G1 (and what’s new in Java 8)
- Serial Communications in Win32
- Serial Communications in Win32
- Serial Communications in Win32
- Serial Communications in Win32
- Serial Communications in Win32
- Serial Communications in Win32
- Concurrent.futures vs Multiprocessing
- Queues in Customer Front End
- Using Delayed queues in practice
- 快速gcd vs 普通gcd
- Concurrent In Java
- 关于cocos2dx RenderTexture的newImage为空
- 酷宝童车书城推荐金融一骗千金(史上十大金融欺诈案)
- android socket通信(下)
- Whois网络注册信息探测
- 【Android】 Viewpager+Fragment实现滑动图片的动态加载
- Concurrent vs serial queues in GCD
- LCD1602.c
- 基于XMPP协议的Android即时通信系
- keyBoard组件
- C语言文件操作函数大全(超详细)
- Eclipse 源码.class file 出现乱码,解决办法之一
- Android基于XMPP Smack openfire 开发的聊天室(一)【会议服务、聊天室列表、加入】
- map 小用法
- timer.c