Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted by Kotenok Gav ( 16 years ago )
#import "TestPropertyAppDelegate.h"

@implementation ThreadFoo

@synthesize x;

- (id)init{
 if (!(self = [super init])){
  return nil;
 }
 self.x = 2010;
 return self;
}

-(void)lockObj:(id)obj{
 // лочимся по объекту на долго
 @synchronized(self){
  NSLog(@"Locking object using @synchronize for a while..");
  [NSThread sleepForTimeInterval:2.5];
  NSLog(@"Unlocking");
 }
}

@end


@implementation TestPropertyAppDelegate

@synthesize window;


- (void)applicationDidFinishLaunching:(UIApplication *)application {    
 ThreadFoo *foo = [[ThreadFoo alloc] init];
 // отдельный поток лочит @synchronize-ом объект на довольно долгое время
 [NSThread detachNewThreadSelector:@selector(lockObj:) toTarget:foo withObject:foo];
 // подождём в нашем потоке децл, что бы уж наверняка залочилось
 [NSThread sleepForTimeInterval:1.0];
 // попытаемся заиметь доступ к атомик-свойству
 NSLog(@"Accessing atomic property: value is %d", foo.x);
 
    [window makeKeyAndVisible];
}


- (void)dealloc {
    [window release];
    [super dealloc];
}


@end

 

Revise this Paste

Your Name: Code Language: