(iphone)这里如何正确设置runloop?很多问题

发布于 2024-10-11 16:27:26 字数 3427 浏览 1 评论 0原文

我刚刚开始阅读苹果的线程编程指南。
我正在启动一个线程并对其进行引用

self.myThread = [[NSThread alloc] initWithTarget: self
                                        selector: @selector(myThreadMain)
                                        object: nil];
[self.myThread start];

,并且 threadMain 看起来像文档中的示例代码。
我需要更改主线程代码中的“exitNow”变量来终止该线程,但不知道如何操作。
myThreadMain循环在不执行任何工作时需要休眠,但也不知道如何实现它。
文档说,如果新线程不会立即终止,我将至少需要一个输入源,但是新线程只需要接收“立即退出消息”和执行选择器:onThread:调用。
我应该设置一个输入源吗?

- (void) myThreadMain
{
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

    BOOL exitNow = NO;
    NSRunLoop* runLoop = [NSRunLoop currentRunLoop];

    // Add the exitNow BOOL to the thread dictionary.                                                                                                                                                                                                                         
    NSMutableDictionary* threadDict = [[NSThread currentThread] threadDictionary];
    [threadDict setValue:[NSNumber numberWithBool:exitNow] forKey:@"ThreadShouldExitNow"];

    // Install an input source.                                                                                                                                                                                                                                               
//  [self myInstallCustomInputSource];                                                                                                                                                                                                                                        

    while (!exitNow)
    {
        // Do one chunk of a larger body of work here.                                                                                                                                                                                                            

        // Run the run loop but timeout immediately if the input source isn't waiting to fire.                                                                                                                                                                                
        [runLoop runUntilDate:[NSDate date]];

        // Check to see if an input source handler changed the exitNow value.                                                                                                                                                                                                 
        exitNow = [[threadDict valueForKey:@"ThreadShouldExitNow"] boolValue];
    }

    [pool release];
}

我将通过

performSelector:onThread:withObject: 

选择器将一些作业交给该线程来执行,需要定义如下,
但也不知道如何实现这一点。

- (void) selectorToPerformInThread
{
  for( int i = 0; i < 10; ++i )
  {
   do something;
   **if(received a new "selectorToPerformInThread" call 
      when we are still in this loop)
          break out and let the new "selectorToPerformInThread" 
          in order to run from the beginning of loop** 
      }
    }

有很多问题......
让我重申一下我的问题。

  1. 如何设置存储在 threadDict 中的“exitNow”变量。
  2. 如何设置线程,使其在不需要工作(不执行选择器时)时休眠。
  3. 我应该设置一个输入源以便线程不会立即终止吗?
  4. 如何设置运行循环,以便每次调用“performSelector”时,相同的选择器(如果正在执行)被取消,并从头开始运行?

提前致谢。

I've just started reading apple's thread programming guide.
I'm starting a thread and have a reference to it

self.myThread = [[NSThread alloc] initWithTarget: self
                                        selector: @selector(myThreadMain)
                                        object: nil];
[self.myThread start];

and the threadMain looks like example code from the doc.
I'll need to change "exitNow" variable in the code from main thread to terminate this thread, but don't know how.
myThreadMain loop needs to sleep when it's not performing any work, but also don't know how to implement it.
The doc says i'll need at least one input source if the new thread won't die immediately, however the new thread needs only to receive "exit now message" and performSelector:onThread: call.
Should I setup an input source?

- (void) myThreadMain
{
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

    BOOL exitNow = NO;
    NSRunLoop* runLoop = [NSRunLoop currentRunLoop];

    // Add the exitNow BOOL to the thread dictionary.                                                                                                                                                                                                                         
    NSMutableDictionary* threadDict = [[NSThread currentThread] threadDictionary];
    [threadDict setValue:[NSNumber numberWithBool:exitNow] forKey:@"ThreadShouldExitNow"];

    // Install an input source.                                                                                                                                                                                                                                               
//  [self myInstallCustomInputSource];                                                                                                                                                                                                                                        

    while (!exitNow)
    {
        // Do one chunk of a larger body of work here.                                                                                                                                                                                                            

        // Run the run loop but timeout immediately if the input source isn't waiting to fire.                                                                                                                                                                                
        [runLoop runUntilDate:[NSDate date]];

        // Check to see if an input source handler changed the exitNow value.                                                                                                                                                                                                 
        exitNow = [[threadDict valueForKey:@"ThreadShouldExitNow"] boolValue];
    }

    [pool release];
}

I'm going to hand some jobs to this thread by

performSelector:onThread:withObject: 

the selector to perform need to be defined as following,
but also don't know how to achieve this.

- (void) selectorToPerformInThread
{
  for( int i = 0; i < 10; ++i )
  {
   do something;
   **if(received a new "selectorToPerformInThread" call 
      when we are still in this loop)
          break out and let the new "selectorToPerformInThread" 
          in order to run from the beginning of loop** 
      }
    }

That's a lot of questions...
Let me reiterate my questions.

  1. How to set "exitNow" variable which is stored in threadDict.
  2. How to setup the thread so that it sleeps when no work(when it's not performing the selector) is needed.
  3. Should I set up an input source so that the thread doesn't die right away?
  4. How to setup run loop so that for every call to "performSelector", the same selector(if it's performing) is canceled, and run it from the beginning?

Thanks in advance.

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

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

发布评论

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

评论(1

黯淡〆 2024-10-18 16:27:26

1/ 通过引用传递 exitNow 变量并将其设置在 threadDict 中,或者传递包含该变量的对象并设置 obj.exitNow = YES;

2/ 当线程完成其工作时让它消亡。您需要一个新线程,只需启动一个新线程

3/ 我不明白您的想法,抱歉

4/ 我认为您需要一个单例 obj,然后在该对象内设置变量,以便线程退出,然后启动一个新线程并调用选择器

1/ Either pass the exitNow variable by reference and set it in the threadDict or pass the object contains the variable and set obj.exitNow = YES;

2/ Just let it die when the thread finishes its job. You need a new thread, just start a new one

3/ I don't get your idea, sorry

4/ I think you need a singleton obj, and then you set the variable inside that object so that the thread exit and then you start a new thread and call the selector

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