如何填充工作线程的命令缓冲区(iOS线程) [英] how to fill the command buffer of the worker thread (iOS threading)

查看:95
本文介绍了如何填充工作线程的命令缓冲区(iOS线程)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在检查RunLoop iOS apple.com )

I was going over the RunLoop iOS documentation and it discusses the idea illustrated here:
(source: apple.com)

在RunLoopSource中,它为客户端线程(即上图中的Main线程)提供以下接口,以向音频缓冲区填充命令和数据,并随后触发所述缓冲区中可用的所有命令:

in the RunLoopSource it provides the following interface for client threads (ie the Main thread in the above illustration) to fill the audio buffer with commands and data, and to subsequently fire all commands available in the said buffer:

// Client interface for registering commands to process
- (void)addCommand:(NSInteger)command withData:(id)data
- (void)fireAllCommandsOnRunLoop:(CFRunLoopRef)runloop 

在add命令方法中,我们只是将命令添加到NSMutableArray数据结构中.

In the add command method we're simply adding commands to an NSMutableArray data structure.

我的问题是我们如何将这些命令封装在变量中,以便它们成为方法.. addCommand 方法中的 data 变量的类型为 id ..例如,我们可以在其中放置一个块吗?这里有最佳做法/示例代码等吗?谢谢.

My question is how can we encapsulate those commands in variables such that they are methods.. the data variable in the addCommand method is of type id.. can we put a block in there for example? Are there any best practices here/sample code etc? thanks.

推荐答案

此技术使数据块过时.并发使用块的好处在于,您可以在系统上进行任意数量的工作,并且鉴于其总的设备范围,可以安排它认为合适的工作在多个内核和线程上.您还可以使用并发的NSOperation并实现一个fifo来接受工作并对其进行处理,但是在这种情况下,将仅存在辅助线程,并且将在系统认为合适的情况下再次调度运行时的线程,因此没有优势.

This technique pre-dates blocks. The beauty about using blocks with concurrency is that you can throw as much work as you want at the system, and given its total device scope can schedule that work on multiple cores and threads as it sees fit. You can also use a concurrent NSOperation and have it implement a fifo to accept work and process it, but in this case there will only be the secondary thread, and it will again be scheduled run time as the system sees fit to giving it, so no advantage over blocks.

这篇关于如何填充工作线程的命令缓冲区(iOS线程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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