-1

次のコードを書いています。

#import <objc/objc.h>
#import <Foundation/Foundation.h>

@interface myT :NSObject
-(void) startT;
-(void) tfun;
@end

@implementation myT 
-(void) tfun
{
 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLog (@"Inside thread Function .. ");
[pool drain];
}

-(void) startT
{
  [NSThread detachNewThreadSelector:@selector(tfun) toTarget:self withObject:nil ];
}
@end

int main ( int argc, char ** argv)
{
   myT *t = [[myT alloc] init];
   [t startT];
   return 0;
} 

コンパイルされ、実行時エラーが発生します。私は何を間違っていますか?私はpthreadに精通しています。スレッドが完了するまでどのように待つことができますか。つまり、pthread_wait のようなものです。

4

1 に答える 1

0

次のような完全なフラグを設定するまで、実行ループをスピンできます。

-(void) startT
{
   [NSThread detachNewThreadSelector:@selector(tfun) toTarget:self withObject:nil ];

   do {

        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];

   } while (!self.completed);
}

そしてあなたの tfun メソッドで

-(void) tfun
{
   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   NSLog (@"Inside thread Function .. ");
   [pool drain];
   self.complete = YES;
}
于 2012-04-14T11:18:48.790 に答える