GCD和线程 [英] GCD and Threads

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

问题描述

我想了解一些有关GCD和线程的信息.

I want to understand something about GCD and Threads.

我的视图控制器中有一个for循环,它要求我的模型执行一些异步网络请求.

I have a for loop in my view controller which asks my model to do some async network request.

因此,如果循环运行5次,该模型将发出5个网络请求.

So if the loop runs 5 times, the model sends out 5 network requests.

考虑到我正在使用NSURLConnection的sendAsyncRequest且将在另外5个线程上调用完成处理程序的事实,声明模型已创建5个线程是否正确?

Is it correct to state that 5 threads have been created by my model considering the fact that I'm using NSURLConnection's sendAsyncRequest and the completion handlers will be called on an additional 5 threads ?

现在,如果我要求我的视图控制器在不同的线程上执行此for循环,并且在循环的每个迭代中,对模型的调用应取决于先前的迭代,我是否会创建线程在这里吗?

Now, If I ask my view controller to execute this for loop on a different thread and in every iteration of the loop, the call to the model should be dependent on the previous iteration, would I be creating an "Inception" of threads here ?

基本上,仅当前一个线程已完全完成时,我才希望对服务器的后续异步请求(完全是指它的所有子线程也应已完成执行.)

Basically, I want the subsequent async requests to my server only if the previous thread has completed entirely (By entirely I mean all of its sub threads should have finished executing too.)

我什至无法正确地提出问题,因为我非常困惑自己. 但是,如果有人可以提供任何帮助,那将是有帮助的.

I can't even frame the question properly because I'm massively confused myself. But if anybody could help with anything, that would be helpful.

推荐答案

在一般情况下声明已经创建了五个线程是不正确的.

It is not correct to state that five threads have been created in the general case.

在线程和块之间没有一对一的映射. GCD是线程池的实现.

There is no one-to-one mapping between threads and blocks. GCD is an implementation of thread pooling.

根据该设备的最佳设置创建了一定数量的线程-在该OS发行版下创建和维护线程的成本,可用处理器核心的数量,它已经拥有的线程数量,但是这些是目前受阻,Apple愿意考虑的任何其他因素都可能是相关的.

A certain number of threads are created according to the optimal setup for that device — the cost of creating and maintaing threads under that release of the OS, the number of processor cores available, the number of threads it already has but which are presently blocked and any other factors Apple cares to factor in may all be relevant.

GCD随后将在这些线程上散布您的块.否则可能会创建新线程.但这不一定.

GCD will then spread your blocks over those threads. Or it may create new threads. But it won't necessarily.

除此之外,队列只是在块之间建立排序的方法.串行分派队列不一定拥有自己的线程.所有并发调度队列不一定拥有自己的线程.但是没有理由相信任何队列集共享任何线程.

Beyond that queues are just ways of establishing the sequencing between blocks. A serial dispatch queue does not necessarily own its own thread. All concurrent dispatch queues do not necessarily own their own threads. But there's no reason to believe that any set of queues shares any threads.

在操作系统的版本之间,为块选择线程的确切方法已发生变化.例如. iOS 4在线程创建方面非常忙碌,而iOS 5+肯定不是这种方式.

The exact means of picking threads for blocks has changed between versions of the OS. E.g. iOS 4 was highly profligate in thread creation, in a way that iOS 5+ definitely haven't been.

GCD只会尝试在这种情况下做的最好的事情.不要浪费时间尝试再次猜测它.

GCD will just try to do whatever is best in the circumstances. Don't waste your time trying to second guess it.

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

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