0

私はメソッドを持っています(それを実行と呼びましょう):

- (void)run
{
    // Do some initialisation

    // Loop until another thread signals it to exit
    while (SHOULD_STILL_LOOP) { ... }

    // Clean up code
}

そして私はそれを次のように呼びます:

[self performSelectorInBackground:@selector(run)];

SHOULD_STILL_LOOPを実装するための最良の方法は何ですか?アトミックプロパティ、NSCondition、ディスパッチセマフォを使用する必要がありますか?

おそらく、いくつかのスタッキーは私にいくつかのアドバイスを提供することができますか?

ありがとう。

4

1 に答える 1

0

これを行う1つの方法を見つけました。それは、NSThreadを使用することです。

- (void)run:(id)obj
{
    while(![[NSThread currentThread] isCancelled]) { ... }
}

- (void)start
{
    self.thread = [[NSThread alloc] initWithTarget:self
                                          selector:@selector(run)
                                            object:self.object];
    [self.thread start];
}

- (void)stop
{
    [self.thread cancel];
    self.thread = nil;
}
于 2012-06-20T22:41:14.957 に答える