【发布时间】:2013-03-01 12:58:15
【问题描述】:
在方法主体上使用@synchronized 指令
-(void)testSynchronizeMethod:(int)value
{
@synchronized(value)
{
int value1 = 100; //sample line 1
int value2 = 120; //sample line 2
[self calledMethod];
}
}
//case 1
-(void)calledMethod
{
NSLog(@"is @synchronized directive applied to this method");
NSLog(@"what happens if I enclose this method with @synchronized directive");
}
**or**
//case 2
-(void)calledMethod
{
@synchronized(value){
NSLog(@"is @synchronized directive applied to this method");
NSLog(@"what happens if I enclose this method with @synchronized directive");
}
}
问: 情况 2 是否围绕 '-(void) calledMethod' 创建了两个互斥锁?
编辑我在使用此类互斥锁时在主线程上收到信号 SIGINT。 如果有人可以建议我出了什么问题,我将附上屏幕截图?
【问题讨论】:
-
@synchronized如何在int上工作??? -
@HotLicks 忽略这一点。刚刚测试过,根据this apple doc 你是正确的。我的错。
-
A SIGINT 意味着线程在等待某事完成时被中断,在这种情况下是为了访问您的互斥锁。我会考虑使用调度回调而不是使用 @synchronized 轮询数据。
-
但是根据苹果文档@synchronized 会自动为您处理所有可能的异常?为什么这个例外不成立?
标签: ios objective-c multithreading