• Which is the best of GCD, NSThread or NSOperationQueue?


    Simple answer:

    1. Use NSThread (or even the pthreads API) when you want or need to have direct control over the threads you create, e.g. you need fine-grained control over thread priorities or are interfacing with some other subsystem that vends/consumes thread objects directly and you need to stay on the same page with it. Such instances are rare, but they do occur, particularly in real-time applications.

    2. Use GCD when your task lends itself well to simple parallelization, e.g. you just want to toss some work "into the background" with very little additional work, you have some data structures that you merely wish to serialize access to (and serial queues are great for doing that in a lockless fashion), you have some for loops that would lend themselves well to parallelization with dispatch_apply(), you have some data sources / timers that GCD's sources API will enable you to deal with easily in the background, etc etc. GCD is quite powerful and you can use it for a lot more than this, but these are all relative 'no brainer' scenarios where you don't want to get caught up in the initialization and setup tasks so much as simply "do basic stuff in parallel".

    3. Use NSOperation when you're already up at the Cocoa API layer (vs writing in straight C to the POSIX APIs) and have more complex operations you want to parallelize. NSOperation allows for subclassing, arbitrarily complex dependency graphs, cancellation and a supports a number of other higher-level semantics that may be useful to you. NSOperation actually uses GCD under the covers so it's every bit as multi-core, multi-thread capable as GCD, though it also brings the Foundation framework along for the ride so if you're hacking at the POSIX layer, you probably want to use option #2.

  • 相关阅读:
    自定义标签——带属性的标签
    自定义标签——第一个自定义标签
    实例练习----电影天堂抓取下载链接
    自定义标签异常错误汇总
    JSp动作指令
    ASP.NET MVC下使用文件上传和IIS7下的默认设置限制了上传大小的方法
    ::before和::after 常见的用法
    .net平台性能很不错的轻型ORM类Dapper
    jQuery插件之ajaxFileUpload
    SSE:服务器发送事件,使用长链接进行通讯 基础学习
  • 原文地址:https://www.cnblogs.com/zhangjie/p/3513735.html
Copyright © 2020-2023  润新知