具有NSOperationQueueDefaultMaxConcurrentOperationCount的线程数 [英] Number of threads with NSOperationQueueDefaultMaxConcurrentOperationCount

查看:173
本文介绍了具有NSOperationQueueDefaultMaxConcurrentOperationCount的线程数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找与NSOperationQueue的后台线程数相关的任何具体信息,其中NSOperationQueue具有创建给定的NSOperationQueueDefaultMaxConcurrentOperationCount最大并发设置.

I'm looking for any concrete info related to the number of background threads an NSOperationQueue with create given the NSOperationQueueDefaultMaxConcurrentOperationCount maximum concurrency setting.

我已经假设采用某种负载监视来确定最合适的线程数,并且在文档中建议使用此设置.我发现队列产生了大约100个后台线程,而我的应用程序(在运行iOS 5.1.1的iPad 3上运行)因SIGABRT而崩溃.我将其减少到一个更可接受的数字(例如3),并且一切正常.

I had assumed that some sort of load monitoring is employed to determine the most appropriate number of threads to spawn, plus this setting is recommended in the docs. What I'm finding is that the queue spawns roughly 100 background threads and my app (running on iPad 3 with iOS 5.1.1) crashes with SIGABRT. I've reduced this to a more acceptable number like 3 and everything is working fine.

任何评论或见解将不胜感激.

Any comments or insight would be appreciated.

推荐答案

我的经验与您的经验相符(尽管不是100个线程;请进行一些配置以确保您确实有多个线程同时运行.我从未见过)它走得那么高).除非您手动管理并发操作的数量,否则NSOperationQueue将倾向于生成太多并发操作. (我还没有看到有人用可测试的代码而不是文档中的推论来驳斥这一点.)对于可能产生大量潜在的并发操作的任何事情,建议使用setMaxConcurrentOperations.虽然不理想,但我经常使用像这样的函数来协助(当然这并不能帮助您在队列之间取得平衡,因此是次优的):

My experience matches yours (though not to 100 threads; do put in some instrumenting to make sure that you really have that many running simultaneously. I've never seen it go quite that high). Unless you manually manage the number of concurrent operations, NSOperationQueue will tend to generate too many concurrent operations. (I have yet to see anyone refute this with testable code rather than inferences from the documentation.) For anything that may generate a large number of potentially concurrent operations, I recommend setMaxConcurrentOperations. While not ideal, I often wind up using a function like this one to assist (this of course doesn't help you balance between queues, so is very sub-optimal):

unsigned int countOfCores() {
  unsigned int ncpu;
  size_t len = sizeof(ncpu);
  sysctlbyname("hw.ncpu", &ncpu, &len, NULL, 0);

  return ncpu;
}

我热切地等待着任何发布真实代码的人,这些代码证明NSOperationQueue自动为CPU绑定的操作执行正确的负载平衡.我发布了一个示例要点来说明我在说什么.如果不调用setMaxConcurrentOperations:,它将在2核iPad 3上生成大约6个并行进程.在这种非常简单的情况下,没有争用或共享资源,这会增加大约10%-15%的开销.在具有争用的更复杂的代码中(尤其是如果操作可能被取消),它会使事情减慢一个数量级.

I eagerly await anyone posting real code demonstrating NSOperationQueue automatically performing correct load balancing for CPU-bound operations. I've posted a sample gist demonstrating what I'm talking about. Without calling setMaxConcurrentOperations:, it will spawn about 6 parallel processes on a 2-core iPad 3. In this very simplistic case with no contention or shared resources, this adds about a 10%-15% overhead. In more complicated code with contention (and particularly if operations might be cancelled), it can slow things down by an order of magnitude.

这篇关于具有NSOperationQueueDefaultMaxConcurrentOperationCount的线程数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆