Introduction to iOS Concurrency and Threading

Kaan Vural
2 min readJul 7, 2020

--

Grand Central Dispatch provides language features to support concurrency on iOS. It works with Dispacth Queue (objects) in our application.

By default, every iOS application has

  • 1 x Main Thread (Serial Queue)
  • 4 x Background Thread (Concurrent Queue)

Main thread is reserved for UI related tasks. And, we are able to create more custom (concurrent or serial) threads. Concurrent and serial queues help us to manage how we execute tasks.

Serial Queue:

Executes tasks one at a time.So, task 2 will not start until 100% of task 1 completed.

  • Slower
  • Predictable execution order
  • Prevents race conditions

Concurrent Queue:

Every task starts at same time. Does not wait for a task to finish before starting the next one.

  • Faster
  • Unpredictable order of completion

In consequence of any DispacthQueue task can be run synchronously or asynchronously (actually those are a separate concept), there are 4 case to be considered.

Case 1: Concurrent queue + Executing asynchronously;

The control returns immediately to the caller. May execute other tasks in parallel to the currently submitted one.

Case 2: Concurrent queue + Executing synchronously;

The caller waits until the task completes. May execute other tasks.

Case 3: Serial queue + Executing asynchronously;

The control returns immediately to the caller. Current task is the only one running on the queue.

Case 4: Serial queue + Executing synchronously;

The caller waits until the task completes. Current task is the only one running on the queue.

  • Submitting tasks synchronously through the main queue, causes a deadlock. (That's true for any serial queue)

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response