2012-04-14 15 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:カウント: '見つからない[デフォルトで有効]

実行中

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) –

答えて

1

宣言したメソッドはenterThread:count:ですが、実装するメソッドはenterThread:です。また、その警告が表示されていますが、私は古いGNUstepランタイム&hellipからしか見ていないと確信しています。しかし、私はそうは思わない。