NSOperationQueue vs NSThread Priority——控制 NSOperationQueue 消耗多少 cpu

发布于 2024-12-03 19:08:35 字数 553 浏览 3 评论 0原文

我需要异步预加载一些纹理和声音效果,以便在我正在开发的 iOS 游戏中创建流畅的用户体验。

我试图在 NSOperationQueue 和 NSThread 之间做出决定,以在游戏过程中加载这些游戏资源。

NSOperationQueue 似乎是一个明显的赢家,并且有很多好处,但我担心对这个“后台”资源加载任务进行有效的优先级控制,以便我可以根据它消耗的周期数来降低它,以免影响帧率。

我看到 NSOperationQueue 有一个“优先级”的概念,但它看起来与 NSThread 的“优先级”概念不同。如果我错了,请纠正我,但 NSThread 的优先级与系统上运行的其他线程相关,而 NSOperation 的优先级是该任务相对于其他排队任务的优先级。

我想知道的是......

有没有一种方法可以控制给定 NSOperationQueue 任务的优先级,以及与当前运行的其他线程相比,它可以消耗多少个周期(而不是相对优先级与其他排队任务)在设备上?

在 NSOperationQueue 的任务中更改当前线程的优先级是否安全? (使用 NSThread 的 +setPriority)

I have some textures and sound effects preloading I need to do asynchronously to create a smooth user experience in an iOS game I am developing.

I am trying to decide between NSOperationQueue and NSThread to load these game resources during gameplay.

NSOperationQueue seems to be a clear winner and have numerous benefits, but I'm concerned about having effective priority control over this 'background' resource loading task, so that I can turn it down in terms of how many cycles it consumes so as not to affect frame-rates.

I see that NSOperationQueue has a concept of 'priority', but it looks different than NSThread's concept of 'priority'. Correct me if I'm wrong, but NSThread's priority is in relation to other threads running on the system, and NSOperation's priority is the priority of that task in relation to the other queued tasks.

What I want to know is....

Is there a way to control the priority of a given NSOperationQueue task in relation to how many cycles (as opposed to relative priority vs. other queued tasks) it can consume compared to other threads currently running on the device?

Is it safe within an NSOperationQueue's task to change the current thread's priority? (with NSThread's +setPriority)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阳光的暖冬 2024-12-10 19:08:35

NSOperation 的实例还有一个“setThreadPriority:”方法以及“setQueuePriority:”。

这绝对是您正在寻找的。

- (void)setThreadPriority:(double)priority

您指定的值将映射到操作系统的优先级值。仅当操作的 main 方法正在执行时,指定的线程优先级才会应用于线程。当操作的完成块正在执行时,它不会被应用。对于您创建自己的线程的并发操作,您必须在自定义启动方法中自行设置线程优先级,并在操作完成时重置原始优先级。

An instance of NSOperation also have a 'setThreadPriority:' method, along with 'setQueuePriority:'.

This is definitively what you are looking for.

- (void)setThreadPriority:(double)priority

The value you specify is mapped to the operating system’s priority values. The specified thread priority is applied to the thread only while the operation’s main method is executing. It is not applied while the operation’s completion block is executing. For a concurrent operation in which you create your own thread, you must set the thread priority yourself in your custom start method and reset the original priority when the operation is finished.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文