zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    pandas 数据类型研究(三)数据类型object与category
    kaggle比赛实践M5-baseline研读
    pd.melt详解--列转行
    kaggle比赛实践M5-数据集介绍
    kaggle比赛实践M5-比赛介绍
    txNLP 335-374
    信息,熵,联合熵,条件熵,互信息(信息增益),交叉熵,相对熵(KL散度)
    框架SpringMVC笔记系列 二 传值
    项目总结笔记系列 Social Hub KT Session1
    读书笔记系列之java性能优化权威指南 一 第一章
  • 原文地址:https://www.cnblogs.com/zhangjie/p/3513735.html
Copyright © 2011-2022 走看看