2012-04-14 141 views
-1

我写下面的代码。NSThread问题如何解决?

#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类。

+1

后错误日志 – Saphrosit 2012-04-14 16:09:51

回答

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; 
}