2008-11-05 22:22:16 +00:00
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
|
|
|
|
static NSArray *array;
|
|
|
|
|
|
|
|
@interface SyncTest : NSObject
|
|
|
|
- (void) sayHello;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation SyncTest
|
|
|
|
- (void) sayHello
|
|
|
|
{
|
2008-11-26 15:06:06 +00:00
|
|
|
NSLog(@"Before the sync block %s\n",[[[NSThread currentThread] description] cString]);
|
2008-11-05 22:22:16 +00:00
|
|
|
@synchronized(array) {
|
2008-11-26 15:06:06 +00:00
|
|
|
NSLog(@"In the sync block %s:%d\n",[[[NSThread currentThread] description] cString], [NSThread isMainThread]);
|
|
|
|
NSLog(@"Waiting five seconds...\n");
|
2008-11-09 09:17:43 +00:00
|
|
|
[NSThread sleepForTimeInterval: 5.0];
|
2008-11-26 15:06:06 +00:00
|
|
|
NSLog(@"Done waiting\n");
|
2008-11-05 22:22:16 +00:00
|
|
|
}
|
2008-11-26 15:06:06 +00:00
|
|
|
NSLog(@"After the sync block %s\n",[[[NSThread currentThread] description] cString]);
|
2008-11-05 22:22:16 +00:00
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
int main (int argc, const char * argv[])
|
|
|
|
{
|
|
|
|
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
SyncTest *st = [[SyncTest alloc] init];
|
|
|
|
array = [NSArray arrayWithObjects: @"Hello World",nil];
|
|
|
|
|
|
|
|
[NSThread detachNewThreadSelector: @selector(sayHello)
|
|
|
|
toTarget: st
|
|
|
|
withObject: nil];
|
|
|
|
[st sayHello];
|
|
|
|
|
|
|
|
[pool drain];
|
|
|
|
return 0;
|
|
|
|
}
|