iOS 锁

1.Synchronized方式

h文件

@interface testLock : NSObject{

   NSString *mStr;

}

- (void)testSynchronized:(NSString *)str;

@implementation testLock

- (void)testSynchronized:(NSString *)str{

   @synchronized (mStr) {

       NSLog(@"testSynchronized_1,str:%@",str);

       NSLog(@"testSynchronized_2,str:%@",str);

       NSLog(@"testSynchronized_3,str:%@",str);

       NSLog(@"testSynchronized_4,str:%@",str);

       mStr = str;

       }

}

main函数:

testLock *tempLock = [[testLock alloc] init:@"temp"];

[tempLock testSynchronized:@"tempLocktestSynchronized"];

dispatch_async(dispatch_get_global_queue(0, 0), ^{

[tempLock testSynchronized:@"test1testSynchronized"];

});

dispatch_async(dispatch_get_global_queue(0, 0), ^{

[tempLock testSynchronized:@"test2testSynchronized"];

});

dispatch_async(dispatch_get_global_queue(0, 0), ^{

[tempLock testSynchronized:@"test3testSynchronized"];

});

输出结果:

2016-06-12 15:20:30.870 TestRuntime[5407:398185] testSynchronized_1,str:tempLocktestSynchronized

2016-06-12 15:20:30.870 TestRuntime[5407:398185] testSynchronized_2,str:tempLocktestSynchronized

2016-06-12 15:20:30.870 TestRuntime[5407:398185] testSynchronized_3,str:tempLocktestSynchronized

2016-06-12 15:20:30.870 TestRuntime[5407:398185] testSynchronized_4,str:tempLocktestSynchronized

2016-06-12 15:20:30.870 TestRuntime[5407:398223] testSynchronized_1,str:test1testSynchronized

2016-06-12 15:20:30.871 TestRuntime[5407:398223] testSynchronized_2,str:test1testSynchronized

2016-06-12 15:20:30.871 TestRuntime[5407:398223] testSynchronized_3,str:test1testSynchronized

2016-06-12 15:20:30.871 TestRuntime[5407:398223] testSynchronized_4,str:test1testSynchronized

2016-06-12 15:20:30.871 TestRuntime[5407:398224] testSynchronized_1,str:test2testSynchronized

2016-06-12 15:20:30.871 TestRuntime[5407:398224] testSynchronized_2,str:test2testSynchronized

2016-06-12 15:20:30.871 TestRuntime[5407:398224] testSynchronized_3,str:test2testSynchronized

2016-06-12 15:20:30.871 TestRuntime[5407:398224] testSynchronized_4,str:test2testSynchronized

2016-06-12 15:20:30.871 TestRuntime[5407:398225] testSynchronized_1,str:test3testSynchronized

2016-06-12 15:20:30.871 TestRuntime[5407:398225] testSynchronized_2,str:test3testSynchronized

2016-06-12 15:20:30.871 TestRuntime[5407:398225] testSynchronized_3,str:test3testSynchronized

2016-06-12 15:20:30.871 TestRuntime[5407:398225] testSynchronized_4,str:test3testSynchronized

如果修改对应m文件的内容为:

- (void)testSynchronized:(NSString *)str{

@synchronized (mStr) {

   mStr = str;

   NSLog(@"testSynchronized_1,str:%@",str);

   NSLog(@"testSynchronized_2,str:%@",str);

   NSLog(@"testSynchronized_3,str:%@",str);

   NSLog(@"testSynchronized_4,str:%@",str);

   }

}

则输出为:

2016-06-12 15:22:52.821 TestRuntime[5475:399682] testSynchronized_1,str:tempLocktestSynchronized

2016-06-12 15:22:52.821 TestRuntime[5475:399682] testSynchronized_2,str:tempLocktestSynchronized

2016-06-12 15:22:52.821 TestRuntime[5475:399682] testSynchronized_3,str:tempLocktestSynchronized

2016-06-12 15:22:52.821 TestRuntime[5475:399682] testSynchronized_4,str:tempLocktestSynchronized

2016-06-12 15:22:52.822 TestRuntime[5475:399717] testSynchronized_1,str:test1testSynchronized

2016-06-12 15:22:52.822 TestRuntime[5475:399718] testSynchronized_1,str:test2testSynchronized

2016-06-12 15:22:52.822 TestRuntime[5475:399717] testSynchronized_2,str:test1testSynchronized

2016-06-12 15:22:52.822 TestRuntime[5475:399719] testSynchronized_1,str:test3testSynchronized

2016-06-12 15:22:52.822 TestRuntime[5475:399718] testSynchronized_2,str:test2testSynchronized

2016-06-12 15:22:52.822 TestRuntime[5475:399717] testSynchronized_3,str:test1testSynchronized

2016-06-12 15:22:52.822 TestRuntime[5475:399719] testSynchronized_2,str:test3testSynchronized

2016-06-12 15:22:52.822 TestRuntime[5475:399719] testSynchronized_3,str:test3testSynchronized

2016-06-12 15:22:52.822 TestRuntime[5475:399718] testSynchronized_3,str:test2testSynchronized

2016-06-12 15:22:52.822 TestRuntime[5475:399717] testSynchronized_4,str:test1testSynchronized

2016-06-12 15:22:52.822 TestRuntime[5475:399719] testSynchronized_4,str:test3testSynchronized

2016-06-12 15:22:52.822 TestRuntime[5475:399718] testSynchronized_4,str:test2testSynchronized

从上面的结果可以看出,在对mStr变量做锁定的时候,对mStr的值的改变的语句位置不同会造成@synchronized内部执行顺序的不一样,从输出看,只要mStr的值已经被修改,则锁就好像被释放了?~

2.NSLock

NSLock * lock = [[NSLock alloc]init];

NSMutableArray * piaos = [NSMutableArray arrayWithArray:@[@"a",@"b",@"c"]];

// 线程1

dispatch_async(dispatch_get_global_queue(0, 0), ^{

[lock lock];

NSLog(@"thread1:%@",piaos);

sleep(3);

[piaos removeObject:@"a"];

[lock unlock];

});

// 线程2

dispatch_async(dispatch_get_global_queue(0, 0), ^{

[piaos removeObject:@"b"];

NSLog(@"thread2:%@",piaos);

});

// 线程3

dispatch_async(dispatch_get_global_queue(0, 0), ^{

[lock lock];

NSLog(@"thread3:%@",piaos);

[lock unlock];

});

输出:

2016-06-12 15:32:54.819 TestRuntime[5515:409045] thread1:(

a,

b,

c

)

2016-06-12 15:32:54.819 TestRuntime[5515:409046] thread2:(

a,

c

)

2016-06-12 15:32:57.819 TestRuntime[5515:409047] thread3:(

c

)

所有加相同锁的内容只能按照 [lock lock]以及[lock unlock]的顺序执行,对没有加锁的代码片段没有影响

3.NSConditionLock

NSConditionLock * lock = [[NSConditionLock alloc]init];

//Thread1

dispatch_async(dispatch_get_global_queue(0, 0), ^{

for (int i = 0; i< 4; i++) {

[lock lock];

NSLog(@"thread1");

[lock unlockWithCondition:i];

sleep(3);

}

});

// Thread2

dispatch_async(dispatch_get_global_queue(0, 0), ^{

NSLog(@"thread2");

});

// Thread3

dispatch_async(dispatch_get_global_queue(0, 0), ^{

[lock lockWhenCondition:3];

NSLog(@"thread3");

[lock unlock];

});

// Thread4

dispatch_async(dispatch_get_global_queue(0, 0), ^{

[lock lockWhenCondition:2];

NSLog(@"thread4");

[lock unlock];

});

输出:

2016-06-12 15:34:45.145 TestRuntime[5531:411155] thread2

2016-06-12 15:34:45.145 TestRuntime[5531:411154] thread1

2016-06-12 15:34:48.146 TestRuntime[5531:411154] thread1

2016-06-12 15:34:51.146 TestRuntime[5531:411154] thread1

2016-06-12 15:34:51.146 TestRuntime[5531:411155] thread4

2016-06-12 15:34:54.148 TestRuntime[5531:411154] thread1

2016-06-12 15:34:54.148 TestRuntime[5531:411156] thread3

满足条件的时候可以lock和unlock,可以随意组合:lock ,lockWhenCondition,unlock,unlockWithCondition

4.NSRecursiveLock

NSRecursiveLock *lock = [[NSRecursiveLock alloc]init];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

static void (^RecursiveMethod)(int);

RecursiveMethod = ^(int value) {

[lock lock];

if (value > 0) {

NSLog(@"value = %d", value);

sleep(1);

RecursiveMethod(value - 1);

}

[lock unlock];

};

RecursiveMethod(5);

});

输出:

2016-06-12 15:36:44.951 TestRuntime[5544:413127] value = 5

2016-06-12 15:36:45.955 TestRuntime[5544:413127] value = 4

2016-06-12 15:36:46.959 TestRuntime[5544:413127] value = 3

2016-06-12 15:36:47.961 TestRuntime[5544:413127] value = 2

2016-06-12 15:36:48.965 TestRuntime[5544:413127] value = 1

如果改用NSLock,发生死锁则会输出:

2016-06-12 15:38:15.877 TestRuntime[5558:414854] value = 52016-06-12 15:38:16.879 TestRuntime[5558:414854] *** -[NSLock lock]: deadlock ('(null)')

2016-06-12 15:38:16.880 TestRuntime[5558:414854] *** Break on _NSLockError() to debug.

NSRecursiveLock会跟踪它被多少次lock。每次成功的lock都必须平衡调用unlock操作。只有所有的锁住和解锁操作都平衡的时候,锁才真正被释放给其他线程获得;

5.NSCondition

NSCondition * lock = [[NSCondition alloc]init];

NSMutableArray * piaos = [NSMutableArray arrayWithArray:@[@"1",@"2",@"3"]];

// Thread1

dispatch_async(dispatch_get_global_queue(0, 0), ^{

[lock lock];

NSLog(@"Thread1,lock done");

[lock wait];

NSLog(@"Thread1,wait done");

[lock unlock];

NSLog(@"Thread1,unlock done");

});

// Thread2

dispatch_async(dispatch_get_global_queue(0, 0), ^{

static void (^clearTheObj)(NSMutableArray * array);

clearTheObj = ^(NSMutableArray * array){

NSLog(@"%lu",(unsigned long)array.count);

if ([array count]>0) {

[array removeObjectAtIndex:0];

}else{

[lock signal];

NSLog(@"Thread2,signal done");

return;

}

sleep(1);

clearTheObj(array);

};

[lock lock];

NSLog(@"Thread2,lock done");

clearTheObj(piaos);

NSLog(@"Thread2,clearTheObj done");

[lock unlock];

NSLog(@"Thread2,unlock done");

});

输出:

2016-06-12 15:39:37.653 TestRuntime[5571:415857] Thread1,lock done

2016-06-12 15:39:37.653 TestRuntime[5571:415858] Thread2,lock done

2016-06-12 15:39:37.653 TestRuntime[5571:415858] 3

2016-06-12 15:39:38.657 TestRuntime[5571:415858] 2

2016-06-12 15:39:39.658 TestRuntime[5571:415858] 1

2016-06-12 15:39:40.658 TestRuntime[5571:415858] 0

2016-06-12 15:39:40.658 TestRuntime[5571:415858] Thread2,signal done

2016-06-12 15:39:40.658 TestRuntime[5571:415858] Thread2,clearTheObj done

2016-06-12 15:39:40.658 TestRuntime[5571:415858] Thread2,unlock done

2016-06-12 15:39:40.658 TestRuntime[5571:415857] Thread1,wait done

2016-06-12 15:39:40.658 TestRuntime[5571:415857] Thread1,unlock done

说明,thread1在lock之后,执行了wait,然后会暂停执行,随后thread2获取到线程操作权;

知道thread2 执行了signal,thread1才会停止wait,并且继续往下执行~

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,723评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,485评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,998评论 0 344
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,323评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,355评论 5 374
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,079评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,389评论 3 400
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,019评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,519评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,971评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,100评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,738评论 4 324
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,293评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,289评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,517评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,547评论 2 354
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,834评论 2 345

推荐阅读更多精彩内容

  • 我的博客, 各位看官有时间赏光 锁 我们在使用多线程的时候多个线程可能会访问同一块资源,这样就很容易引发数据错乱和...
    VIC_LI阅读 1,513评论 0 36
  • 本文首发在我的个人博客:http://blog.shenyuanluo.com/,喜欢的朋友欢迎订阅。 最近公司有...
    shenyuanluo阅读 3,209评论 4 3
  • iOS开发中知道的哪些锁? 哪个性能最差? 锁是线程编程同步工具的基础。iOS开发中常用的锁有如下几种: @syn...
    linbj阅读 106评论 0 1
  • 一、互斥锁 百度百科:在编程中,引入了对象互斥锁的概念,来保证共享数据操作的完整性。每个对象都对应于一个可称为" ...
    MangK阅读 7,429评论 1 29
  • 本文所讲为其中之一:iOS程序进入后台后十分钟之内就会被系统kill掉,怎么解决呢?我想要程序进入后台后仍然运行计...
    husky_duang阅读 6,546评论 2 4