2012-04-14 75 views
0

以下代碼按預期編譯和執行。NSThread線程安全實現初始化。

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

BOOL loopValue = YES; 
@interface myThread:NSObject 
-(void) enterThread: (NSArray *) elemt count: (NSString *) x; 
@end 

@implementation myThread 
-(void) enterThread : (NSArray *) elemt 
{ 
    NSLog (@" Inside mythread "); 
    NSAutoreleasePool *pool = [[ NSAutoreleasePool alloc] init]; 
int i; 
int cnt =10; 
    for(i=0; i<cnt; i++) { 
    NSLog (@"Number of elemennts in array %i ", [elemt count]); 
    [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]]; 
    } 

    loopValue = NO; 
    [pool drain]; 
} 
@end 



int main (int argc, char ** argv) 
{ 
    NSAutoreleasePool *pool = [[ NSAutoreleasePool alloc] init]; 
    // id tobj = [[myThread alloc] init]; 
    id tobj = [ myThread new ]; 
    NSLog (@"Starting New Thread "); 
    [NSThread detachNewThreadSelector:@selector(enterThread:) toTarget:tobj withObject:[NSArray arrayWithObjects:@"ram",@"20",nil]]; 
    while(1) 
    if (loopValue) 
    [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:2]]; 
    else 
     break; 


    NSLog (@".. Exiting.. \n"); 
    [pool drain]; 
    return 0; 

} 

我的問題:

雖然編譯我得到以下警告..

mythread.m:24:1: warning: incomplete implementation of class ‘myThread’ [enabled by default] 

mythread.m:24:1:警告:對於「方法定義-enterThread: count:'未找到[默認啓用]

執行時

WARNING your program is becoming multi-threaded, but you are using an ObjectiveC runtime library .... Removed due to redability]hich does not have a thread-safe implementation of the +initialize method. ...... 

我是什麼東錯了?如何避免警告/運行時錯誤。

+0

可能重複的[NSThread問題如何解決?](http://stackoverflow.com/questions/10153116/nsthread-issue-how-to-resolve) – 2012-04-14 17:52:04

回答

1

您聲明的方法是enterThread:count:,但實現的方法是enterThread:。此外,你得到的警告,我確信我只看到從舊的GNUstep運行時&hellip;但我想不是。