Skip to main content

Main thread and main queue: what’s the difference?

About 2 minSwiftArticle(s)bloghackingwithswift.comcrashcourseswiftxcodeappstore

Main thread and main queue: what’s the difference? 관련

Swift Concurrency by Example

Back to Home

Main thread and main queue: what’s the difference? | Swift Concurrency by Example

Main thread and main queue: what’s the difference?

Updated for Xcode 15

The main thread is the one that starts our program, and it’s also the one where all our UI work must happen. However, there is also a main queue, and although sometimes we use the terms “main thread” and “main queue” interchangeably, they aren’t quite the same thing.

It’s a subtle distinction, but it can sometimes matter: although your main queue will always execute on the main thread (and is therefore where you’ll be doing your UI work!), it’s also possible that other queues might sometimes run on the main thread – the system is free to move things around in whatever way is most efficient.

So, if you’re on the main queue then you’re definitely on the main thread, but being on the main thread doesn’t automatically mean you’re on the main queue – a different queue could temporarily be running on the main thread.

At this point you’re very likely staring at the screen wondering when this would ever be a problem, or perhaps even rereading what I said like it’s a cryptic riddle. Trust me, if you ever hit a problem where the main thread vs main queue matters, you’ll be glad you read this!

Similar solutions…
How to use @MainActor to run code on the main queue | Swift Concurrency by Example

How to use @MainActor to run code on the main queue
Understanding threads and queues | Swift Concurrency by Example

Understanding threads and queues
What’s the difference between a task and a detached task? | Swift Concurrency by Example

What’s the difference between a task and a detached task?
What’s the difference between async let, tasks, and task groups? | Swift Concurrency by Example

What’s the difference between async let, tasks, and task groups?
What’s the difference between actors, classes, and structs? | Swift Concurrency by Example

What’s the difference between actors, classes, and structs?

이찬희 (MarkiiimarK)
Never Stop Learning.