保持 NSThread 处于活动状态并在其上运行 NSRunLoop

发布于 2024-11-14 02:12:23 字数 947 浏览 5 评论 0原文

因此,我正在启动一个新的 NSThread,我希望稍后能够通过调用 performSelector:onThread:... 来使用它。根据我对它的理解,调用该方法将该调用添加到该线程上的运行循环中,因此在下一次迭代中,它将弹出所有这些调用,然后调用它们,直到没有什么可调用的为止。所以我需要这种功能,一个准备工作的空闲线程,我可以调用它。我当前的代码如下所示:

   - (void)doInitialize
   {
       mThread =  [[NSThread alloc] initWithTarget:self selector:@selector(runThread) object:nil];
       [mthread start];
   }

   - (void)runThread
   {
       NSAutoReleasePool *pool = [[NSAutoReleasePool alloc] init];

       // From what I understand from the Google machine is that this call should start the
       // runloop on this thread, but it DOESN'T. The thread dies and is un-callable
       [[NSRunLoop currentRunLoop] run];

       [pool drain];
   }

   - (void)scheduleSomethingOnThread
   {
       [self performSelector:@selector(hardWork) onThread:mThread withObject:nil waitUntilDone:NO];
   }

但是线程没有保持活动状态,并且 PerformSelector:onThread 不执行任何操作。我该如何以正确的方式解决这个问题?

So I'm starting a new NSThread that I want to be able to use later by calling performSelector:onThread:.... From how I understand it calling that methods add that call to the runloop on that thread, so on its next iteration it will pop all these calls and subsequently call them until there is nothing left to call. So I need this kind of functionality, an idle thread ready for work that I just can call upon it. My current code looks like this:

   - (void)doInitialize
   {
       mThread =  [[NSThread alloc] initWithTarget:self selector:@selector(runThread) object:nil];
       [mthread start];
   }

   - (void)runThread
   {
       NSAutoReleasePool *pool = [[NSAutoReleasePool alloc] init];

       // From what I understand from the Google machine is that this call should start the
       // runloop on this thread, but it DOESN'T. The thread dies and is un-callable
       [[NSRunLoop currentRunLoop] run];

       [pool drain];
   }

   - (void)scheduleSomethingOnThread
   {
       [self performSelector:@selector(hardWork) onThread:mThread withObject:nil waitUntilDone:NO];
   }

But the thread is not kept alive, and the performSelector:onThread does not do anything. How do I go about this the right way?

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

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

发布评论

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

评论(2

薄情伤 2024-11-21 02:12:23

运行循环至少需要一个“输入源”才能运行。主运行循环可以,但您必须手动添加源才能获取辅助运行循环的 -run 方法来执行任何操作。有一些关于此的文档 此处

让它发挥作用的一种简单方法是将 [[NSRunLoop currentRunLoop] run] 放入无限循环中;当有事情要做时,它会做,否则立即返回。问题是线程将花费相当多的处理器时间来等待某些事情发生。

另一个解决方案是在此运行循环上安装 NSTimer 以使其保持活动状态。

但是,如果可能的话,您应该使用专为此类事情设计的机制。如果可能,您可能希望使用 NSOperationQueue 进行后台操作。

A run loop requires at least one "input source" to run. The main run loop does, but you have to add a source manually to get a secondary run loop's -run method to do anything. There's some documentation on this here.

One naïve way to get this to work would be just to put [[NSRunLoop currentRunLoop] run] in an infinite loop; when there's something to do, it'll do it, and return immediately otherwise. The problem is that the thread will take a decent amount of processor time simply waiting for something to occur.

Another solution is to install an NSTimer on this run loop to keep it alive.

But, if possible, you should use a mechanism designed for this sort of thing. If possible, you may want to use NSOperationQueue for background operations.

你与昨日 2024-11-21 02:12:23

这段代码应该强制线程永远等待

BOOL shouldKeepRunning = YES;        // global
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[runLoop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode]; // adding some input source, that is required for runLoop to runing
while (shouldKeepRunning && [runLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]); // starting infinite loop which can be stopped by changing the shouldKeepRunning's value

this piece of code should force the thread to wait forever

BOOL shouldKeepRunning = YES;        // global
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[runLoop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode]; // adding some input source, that is required for runLoop to runing
while (shouldKeepRunning && [runLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]); // starting infinite loop which can be stopped by changing the shouldKeepRunning's value
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文