澄清在螺纹和奔跑循环在可可粉 [英] Clarification on Threads and Run Loops In Cocoa
问题描述
我想学习线程,我完全困惑。我相信所有的答案都在苹果文档,但我只是发现它真的很难分解和消化。也许有人可以为我清除一个东西或2。
1)performSelectorOnMainThread
一个事件在主运行循环或者是某种方式一个新的线程,即使该方法说mainThread?如果线程的目的是缓解主线程上的处理,这有什么帮助?
2)RunLoops
是真的,如果我想创建一个完全独立的线程,我使用
detachNewThreadSelector?调用start是否启动已经创建的线程的默认运行循环?如果是这样,运行循环到哪里呢?
3)最后,我看到使用NSOperationQueue的例子。是真的说如果你使用performSelectorOnMainThread的线程都在队列中反正因此NSOperation不需要?
4)我应该忘记所有这一切,只是使用
运行循环
您可以将Run Loop看作是与线程相关联的事件处理for-loop。这是由系统为每个线程提供的,但它只为主线程自动运行。
请注意,运行运行循环和执行线程是两个截然不同的概念。你可以执行一个线程而不运行一个运行循环,当你只是执行长的计算,你不必响应各种事件。
如果你想响应辅助线程中的各种事件,可以通过
[NSRunLoop currentRunLoop]
并运行它。事件运行循环可以处理的是调用输入源。
PerformSelector
performSelectorOnMainThread:
将目标和选择器添加到名为 performSelector input source 的特殊输入源。
NSOperation / NSOperationQueue
/ strong>我认为 NSOperation
是一种在应用程序中显式声明各种任务的方法,需要一些时间但可以大多数独立运行。它更容易使用比分离新线程自己和维护各种东西自己,太。主要的 NSOperationQueue
会自动维护一组可重用的后台线程,并且并行运行 NSOperations
。
所以是的,如果你只需要排队在主线程中的操作,你可以取消 NSOperationQueue
,只需使用 performSelectorOnMainThread:
,但这不是 NSOperation
的要点。
GCD
GCD是一种在Snow Leopard中引入的新基础架构。 现在在其上实现NSOperationQueue
。
它在功能/块的级别工作。给 dispatch_async
的进给块是非常方便的,但是对于更大的操作块我喜欢使用 NSOperation
,特别是当
您需要阅读 Apple的官方文档!这一点上也有许多信息丰富的博客文章。
I'm trying to learn about threading and I'm thoroughly confused. I'm sure all the answers are there in the apple docs but I just found it really hard to breakdown and digest. Maybe somebody could clear a thing or 2 up for me.
1)performSelectorOnMainThread
Does the above simply register an event in the main run loop or is it somehow a new thread even though the method says "mainThread"? If the purpose of threads is to relieve processing on the main thread how does this help?
2) RunLoops
Is it true that if I want to create a completely seperate thread I use "detachNewThreadSelector"? Does calling start on this initiate a default run loop for the thread that has been created? If so where do run loops come into it?
3) And Finally , I've seen examples using NSOperationQueue. Is it true to say that If you use performSelectorOnMainThread the threads are in a queue anyway so NSOperation is not needed?
4) Should I forget about all of this and just use the Grand Central Dispatch instead?
Run Loops
You can think of a Run Loop to be an event processing for-loop associated to a thread. This is provided by the system for every thread, but it's only run automatically for the main thread.
Note that running run loops and executing a thread are two distinct concepts. You can execute a thread without running a run loop, when you're just performing long calculations and you don't have to respond to various events. If you want to respond to various events from a secondary thread, you retrieve the run loop associated to the thread by
[NSRunLoop currentRunLoop]
and run it. The events run loops can handle is called input sources. You can add input sources to a run-loop.
PerformSelector
performSelectorOnMainThread:
adds the target and the selector to a special input source called performSelector input source. The run loop of the main thread dequeues that input source and handles the method call one by one, as part of its event processing loop.
NSOperation/NSOperationQueue
I think of NSOperation
as a way to explicitly declare various tasks inside an app which takes some time but can be run mostly independently. It's easier to use than to detach the new thread yourself and maintain various things yourself, too. The main NSOperationQueue
automatically maintains a set of background threads which it reuses, and run NSOperations
in parallel.
So yes, if you just need to queue up operations in the main thread, you can do away with NSOperationQueue
and just use performSelectorOnMainThread:
, but that's not the main point of NSOperation
.
GCD
GCD is a new infrastructure introduced in Snow Leopard. NSOperationQueue
is now implemented on top of it.
It works at the level of functions / blocks. Feeding blocks to dispatch_async
is extremely handy, but for a larger chunk of operations I prefer to use NSOperation
, especially when that chunk is used from various places in an app.
Summary
You need to read Official Apple Doc! There are many informative blog posts on this point, too.
这篇关于澄清在螺纹和奔跑循环在可可粉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!