保留对 NSThread 的引用并向其对象发送消息?

发布于 2024-08-30 14:05:50 字数 769 浏览 7 评论 0原文

我有点不确定如何执行此操作:

我启动一个“工作线程”,该线程在我的应用程序“生命周期”期间运行。

[NSThread detachNewThreadSelector:@selector(updateModel) toTarget:self withObject:nil];

然后

- (void) updateModel {

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    BackgroundUpdate *update = [[BackgroundUpdate alloc] initWithTimerInterval:5];
    [[NSRunLoop currentRunLoop] run];   //keeps it going 'forever'
    [update release];
    [pool release];
}

现在线程每 5 秒“唤醒”一次(initWithTimerInterval)以查看是否 它可以执行任何任务。 BackGroundUpdate 类中的所有任务目前仅与时间相关。我想要一些“事件相关”的。例如,我想从主线程调用后台对象,并告诉它“speedUp”、“slowDown”、“reset”或对象上的任何方法。

为此,我想我需要类似 performSelectorOnThread 的东西,但是如何获取对 NSthread 和背景对象的引用?

I am a bit uncertain on how to do this:

I start a "worker-thread" that runs for the duration of my apps "life".

[NSThread detachNewThreadSelector:@selector(updateModel) toTarget:self withObject:nil];

then

- (void) updateModel {

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    BackgroundUpdate *update = [[BackgroundUpdate alloc] initWithTimerInterval:5];
    [[NSRunLoop currentRunLoop] run];   //keeps it going 'forever'
    [update release];
    [pool release];
}

Now the thread "wakes" up every 5 seconds(initWithTimerInterval) to see if
there are any tasks it can do. All the tasks in the BackGroundUpdate Class are only time dependent for now. I would like to have a few that were "event dependent". e.g. I would like to call the Background Object from my main thread and tell it to "speedUp", "slowDown", "reset" or any method on the object.

To do this I guess I need something like performSelectorOnThread but how to get a reference to the NSthread and the Background Object?

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

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

发布评论

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

评论(1

带上头具痛哭 2024-09-06 14:05:50

直接回答:不要使用+[NSThread detachNewThreadSelector:toTarget:withObject:],而是使用[[NSThread alloc] initWithTarget:selector:object:]。不要忘记拨打-start!

其他想法:

  • 考虑使用 NSOperation/NSOperationQueue 代替。对于大多数工作线程的使用来说更容易、更高效。
  • 考虑一下您是否真的需要对后台线程进行定期检查。您能否只在主运行循环上执行此操作,然后根据需要将工作转移到其他线程上?线程不是免费的。
  • 还要考虑轮询是否是最佳实现。研究 NSCondition 和/或 NSConditionLock 以获得更有效的方法来唤醒线程,当发生某些事情时(例如将工作添加到队列中),无需轮询。

Direct answer: Instead of +[NSThread detachNewThreadSelector:toTarget:withObject:], use [[NSThread alloc] initWithTarget:selector:object:]. Don't forget to call -start!

Other thoughts:

  • Consider using NSOperation/NSOperationQueue instead. Easier and more efficient for most worker thread uses.
  • Consider whether you really need to do that periodic check on a background thread. Could you just do it on the main run loop, and then throw off work onto other threads as needed? Threads aren't free.
  • Consider whether polling is the best implementation, too. Look into NSCondition and/or NSConditionLock for more efficient ways to wake up threads when something happens (like adding work to a queue), no polling necessary.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文