NSThread线程安全实现初始化。?

时间:2012-04-14 12:40:04

标签: objective-c ios

以下代码按预期编译和执行。

#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. ......

我错了什么?如何避免警告/运行时错误。

1 个答案:

答案 0 :(得分:1)

您声明的方法是enterThread:count:,但您实施的方法是enterThread:。此外,你得到的警告,我确信我只是从旧的GNUstep运行时看到的......但我猜不是。