Whoami
Whoami

Reputation: 14408

NSThread thread-safe implementation of initialize.?

The following code compiles and executes as expected.

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

}

MY Question:

While compilation i do get the following warnings..

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

mythread.m:24:1: warning: method definition for ‘-enterThread:count:’ not found [enabled by default]

While Execution

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

What am i dong wrong ? how to avoid both warning/runtime errors.

Upvotes: 0

Views: 395

Answers (1)

dreamlax
dreamlax

Reputation: 95335

The method you declared is enterThread:count: but the method you implement is enterThread:. Also, that warning you are getting, I'm sure I've only seen that from the old GNUstep runtime… but I guess not.

Upvotes: 1

Related Questions