# iOS Concurrency with GCD and Operations - Part 2: | Ray Wenderlich

**URL:** <https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-2-ray-wenderlich/23782>\
**Category:** 🎓 Courses & Articles\
**Created:** [December 6, 2016, 12:24pm UTC](https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-2-ray-wenderlich/23782 "2016-12-06T12:24:43Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![audrey](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/audrey/32/111607_2.png) [@audrey](https://forums.kodeco.com/u/audrey)\
**Post date:** [December 6, 2016, 12:24pm UTC](https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-2-ray-wenderlich/23782/1 "2016-12-06T12:24:43Z")

</div>

Learn the basics of concurrency and Grand Central Dispatch — serial and concurrent queues, synchronous and asynchronous dispatch.

* * *
 This is a companion discussion topic for the original entry at [https://www.raywenderlich.com/3648-ios-concurrency-with-gcd-and-operations/lessons/2](https://www.raywenderlich.com/3648-ios-concurrency-with-gcd-and-operations/lessons/2)

---

<div class="post-metadata">

**Author:** ![audrey](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/audrey/32/111607_2.png) [@audrey](https://forums.kodeco.com/u/audrey)\
**Post date:** [December 6, 2016, 9:06pm UTC](https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-2-ray-wenderlich/23782/2 "2016-12-06T21:06:40Z")

</div>

Links mentioned in this video:

- [Apple’s Energy Efficiency Guide for iOS Apps](https://developer.apple.com/library/content/documentation/Performance/Conceptual/EnergyGuide-iOS/PrioritizeWorkWithQoS.html#//apple_ref/doc/uid/TP40015243-CH39-SW1)
- [GCD’s Main Queue vs. Main Thread](http://blog.benjamin-encz.de/post/main-queue-vs-main-thread/)

---

<div class="post-metadata">

**Author:** ![createch](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/createch/32/109646_2.png) [@createch](https://forums.kodeco.com/u/createch)\
**Post date:** [December 8, 2016, 12:02am UTC](https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-2-ray-wenderlich/23782/3 "2016-12-08T00:02:48Z")

</div>

I am trying to run the playground provided (Terminology) (both the starter that I have edited and the finished one provided) and when I do that, I get no output and the playground continues to run and does not stop.  
I must be missing something. Can you help?

---

<div class="post-metadata">

**Author:** ![audrey](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/audrey/32/111607_2.png) [@audrey](https://forums.kodeco.com/u/audrey)\
**Post date:** [December 8, 2016, 12:31am UTC](https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-2-ray-wenderlich/23782/4 "2016-12-08T00:31:55Z")

</div>

I’m using Xcode 8.1. I downloaded what you would’ve gotten and it runs ok.

Try quitting Xcode and restarting. If that doesn’t work, try restarting your Mac?

oh yes, I’m on Sierra

---

<div class="post-metadata">

**Author:** ![createch](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/createch/32/109646_2.png) [@createch](https://forums.kodeco.com/u/createch)\
**Post date:** [December 8, 2016, 12:51am UTC](https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-2-ray-wenderlich/23782/5 "2016-12-08T00:51:50Z")

</div>

Thanks for your very quick response. I was using xCode 8.2 beta but when I switched to 8.1 it works fine. Now, it could be that I have a messed up install with multiple versions of xCode so others may not experience the same issue with 8.2 beta  
I noticed that when I ran it in 8.1 it first indicated that it was launching the simulator so I could have broken that piece in my 8.2 install.  
Thanks again for the quick response.

---

<div class="post-metadata">

**Author:** ![audrey](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/audrey/32/111607_2.png) [@audrey](https://forums.kodeco.com/u/audrey)\
**Post date:** [December 8, 2016, 2:37am UTC](https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-2-ray-wenderlich/23782/6 "2016-12-08T02:37:45Z")

</div>

Xcode 8 betas seem to be doing strange things, even if they’re not open. Some of us were complaining in a slack channel that the command-/ to comment had stopped working in the released Xcode 8. Then one guy discovered that deleting the beta from his Mac fixed the problem — he wasn’t using it, it was just sitting there, unopened! Sure enough, I deleted my beta copy and I could comment lines again!

---

<div class="post-metadata">

**Author:** ![lars](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/lars/32/106129_2.png) [@lars](https://forums.kodeco.com/u/lars)\
**Post date:** [December 18, 2016, 10:16pm UTC](https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-2-ray-wenderlich/23782/7 "2016-12-18T22:16:13Z")

</div>

There is an error in the ChallangeFinished code…  
The asyncTiltShift function runs the completion handler on the runQueue and never uses completionQueue…

---

<div class="post-metadata">

**Author:** ![audrey](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/audrey/32/111607_2.png) [@audrey](https://forums.kodeco.com/u/audrey)\
**Post date:** [December 18, 2016, 10:51pm UTC](https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-2-ray-wenderlich/23782/8 "2016-12-18T22:51:29Z")

</div>

good catch! I should’ve set `resultQueue` to the default queue, run `completion(outputImage)` in `resultQueue`, and noted again that the main queue behaves strangely in playgrounds.

I’ll see if we can replace the ChallengeFinished soon.

edited to add: I actually set `resultQueue` correctly in the challenge starter, but forgot to update the finished playground 😳

---

<div class="post-metadata">

**Author:** ![lars](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/lars/32/106129_2.png) [@lars](https://forums.kodeco.com/u/lars)\
**Post date:** [December 19, 2016, 8:29am UTC](https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-2-ray-wenderlich/23782/9 "2016-12-19T08:29:29Z")

</div>

👍 Great series so far. Looking forward to the rest!

---

<div class="post-metadata">

**Author:** ![keshv](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/keshv/32/79544_2.png) [@keshv](https://forums.kodeco.com/u/keshv)\
**Post date:** [January 29, 2017, 8:55am UTC](https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-2-ray-wenderlich/23782/10 "2017-01-29T08:55:02Z")

</div>

@audrey You have mentioned in this video that iPhone and iPads before 2011 were having single processors. So on those phones **Serial and Concurrent queues will be same in terms of functionality?** My reasoning behind it is, there only context switching will happen as more than one thread can’t run simultaneously. So if it is true, then priority given to main thread will be far far more than in phones having multicore as UI should have minimal impact of single Core.If i am using Concurrent queue and my app is running on single core phone, then would it have a negative impact? Also can context switching happen between tasks in serial queue or they will be always excecuted in FIFO order from Start to completion?

---

<div class="post-metadata">

**Author:** ![audrey](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/audrey/32/111607_2.png) [@audrey](https://forums.kodeco.com/u/audrey)\
**Post date:** [January 29, 2017, 11:46am UTC](https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-2-ray-wenderlich/23782/11 "2017-01-29T11:46:31Z")

</div>

hi Keshav: yes, concurrency on a single CPU is achieved by multiplexing / context switching. User interaction doesn’t actually require much CPU attention: people are pretty slow compared with a CPU. I found this account by Oleg Andreev, who did a little experiment with a single-CPU iPhone 4: [Oleg Andreev - Fun with concurrency on a single core](http://blog.oleganza.com/post/15786254008/fun-with-concurrency-on-a-single-core).

> [When] the app was rendering graphics on the main thread. 90% of CPU time was spent on graphics, 10% on gesture recognition and related computations. Overall CPU utilization was about 30%… [after moving OpenGL rendering to serial dispatch queue] the rendering was indeed slightly smoother, but touches were still delayed… 70% of CPU time was spent on graphics (in background thread) and 30% was spent on gesture recognition. Also, overall CPU usage increased up to 50%.

Tasks in a serial queue run FIFO in the sense of first- **ready** -first-run.

---

<div class="post-metadata">

**Author:** ![preethambaliga](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/preethambaliga/32/105896_2.png) [@preethambaliga](https://forums.kodeco.com/u/preethambaliga)\
**Post date:** [March 2, 2017, 5:27am UTC](https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-2-ray-wenderlich/23782/12 "2017-03-02T05:27:58Z")

</div>

Hi Audrey,  
I understand making use of sync to avoid race condition. But is it necessary to use a serial queue ?  
Can we do the same thing on a concurrent queue and avoid race condition ?

---

<div class="post-metadata">

**Author:** ![audrey](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/audrey/32/111607_2.png) [@audrey](https://forums.kodeco.com/u/audrey)\
**Post date:** [March 2, 2017, 5:55am UTC](https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-2-ray-wenderlich/23782/13 "2017-03-02T05:55:18Z")

</div>

hi Preetham: in part 11, we use a dispatch barrier with a concurrent queue to make a variable thread safe.

---

<div class="post-metadata">

**Author:** ![preethambaliga](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/preethambaliga/32/105896_2.png) [@preethambaliga](https://forums.kodeco.com/u/preethambaliga)\
**Post date:** [March 2, 2017, 6:33am UTC](https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-2-ray-wenderlich/23782/14 "2017-03-02T06:33:25Z")

</div>

okay. Thanks a lot for the quick response. Right now in part 2… Will get back if I have any queries . Thanks a lot

---

<div class="post-metadata">

**Author:** ![daiwenx](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@daiwenx](https://forums.kodeco.com/u/daiwenx)\
**Post date:** [April 8, 2017, 3:23pm UTC](https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-2-ray-wenderlich/23782/15 "2017-04-08T15:23:34Z")

</div>

I have doubt about how `sync` removes race condition, in the example:

```
value = 42
mySerialQueue.sync {
  changeValue()
}
value

```

As you explained earlier in the video, `sync` actually runs the code in the current thread, isn’t it simpler just call it directly? Like this:

```
value = 42
changeValue()
value

```

Thanks for you further explanation.

---

<div class="post-metadata">

**Author:** ![audrey](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/audrey/32/111607_2.png) [@audrey](https://forums.kodeco.com/u/audrey)\
**Post date:** [April 9, 2017, 12:03am UTC](https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-2-ray-wenderlich/23782/16 "2017-04-09T00:03:11Z")

</div>

hi Daiwen: `sync` blocks the current thread while the task runs on `mySerialQueue`. So, in this example, the `value` statement doesn’t run until `changeValue()` has finished.

In the `async` version, the `value` statement runs immediately, so shows the old value.

---

<div class="post-metadata">

**Author:** ![vituan99](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/vituan99/32/81494_2.png) [@vituan99](https://forums.kodeco.com/u/vituan99)\
**Post date:** [September 19, 2017, 8:19am UTC](https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-2-ray-wenderlich/23782/17 "2017-09-19T08:19:19Z")

</div>

hi Audrey, thank for your tutorial.  
I can not find different sync - serial and sync - concurrent.  
I see it works the same.

---

<div class="post-metadata">

**Author:** ![audrey](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/audrey/32/111607_2.png) [@audrey](https://forums.kodeco.com/u/audrey)\
**Post date:** [September 20, 2017, 1:44am UTC](https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-2-ray-wenderlich/23782/18 "2017-09-20T01:44:55Z")

</div>

there’s a nice example at [ios - DispatchQueue Sync Concurrent - Stack Overflow](https://stackoverflow.com/questions/44429221/dispatchqueue-sync-concurrent)

when you dispatch sync to a concurrent queue from two loops, the results can appear in a different order on each run, because the concurrent queue can run more than one task at a time, and you get a race condition

when you dispatch sync to a serial queue from the two loops, the results appear in alternate order each time, because the tasks run in exactly the same order they get added to the serial queue

the `usleep(100)` seems to favour the second loop: the runner1 result always appears first; if I comment out the sleeps, the runner0 result appears first.

---

<div class="post-metadata">

**Author:** ![mike](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/mike/32/102075_2.png) [@mike](https://forums.kodeco.com/u/mike)\
**Post date:** [February 1, 2018, 1:08pm UTC](https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-2-ray-wenderlich/23782/19 "2018-02-01T13:08:10Z")

</div>

Hi Audrey, great tutorial! Very clear and concise. I finished the whole course and reorganized my own knowledge and code ever used at work.

My other previous co-workers ever used DispatchSemaphore, is it old Objective-C thread safe tools, or part of GCD? Can I use serial queue to replace semaphore in the project? Thanks!

---

<div class="post-metadata">

**Author:** ![audrey](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/audrey/32/111607_2.png) [@audrey](https://forums.kodeco.com/u/audrey)\
**Post date:** [February 1, 2018, 9:13pm UTC](https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-2-ray-wenderlich/23782/20 "2018-02-01T21:13:53Z")

</div>

hi Mike! DispatchSemaphore is part of GCD, and is still useful.

[Soroush Khanlou’s GCD Handbook](http://khanlou.com/2016/04/the-GCD-handbook/) shows a couple of uses.

However, [Rob’s stack overflow answer](https://stackoverflow.com/questions/4326350/how-do-i-wait-for-an-asynchronously-dispatched-block-to-finish) advises against over-use of dispatch semaphores:

> while the dispatch semaphore technique is a wonderful technique when absolutely needed, I must confess that I see too many new developers, unfamiliar with good asynchronous programming patterns, gravitate too quickly to semaphores as a general mechanism for making asynchronous routines behave synchronously.

And [WWDC 2015](https://developer.apple.com/videos/play/wwdc2015/718/?time=1646) and [2016 GCD](https://developer.apple.com/videos/play/wwdc2016-720/?time=952) talks have pointed out that Apple’s priority inversion fix (promoting lower priority tasks) doesn’t work with dispatch semaphores, because they don’t have an owning task, so the system can’t tell where they’re being triggered.

Earlier in the [WWDC 2015 talk](https://developer.apple.com/videos/play/wwdc2015-718/?time=1506), they discuss the use of serial queues as locks. Serial queues **can** take advantage of the priority inversion fix, so if that’s a factor in your project, you should try to use a serial queue. OTOH if it’s not broken … ;]

[Next page](https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-2-ray-wenderlich/23782.md?page=2)
