Search code examples
iosmultithreadinggrand-central-dispatch

When should I use Thread but DispatchQueue on iOS?


In my daily work, I can handle some multi-thread condition using GCD, but I also see the usage of thread in project, example, log module use thread but queue in my project. I just can't understand why. There is some benefit to use thread than GCD? How should I choose?


Solution

  • pthread is often used by cross-platform code, so you are quite likely to run into it when you include third-party libraries in your code, and usually don't touch it.

    Using NSThread would be very, very, very rare. Except for calling [NSThread isMainThread] to determine whether your code is running on the main thread or on a background thread. Unless you have a real good reason to do otherwise, use GCD. Mostly because it is so much easier to use that suddenly you will use multithreading in places where you would never have used it otherwise.