EdwardEan 最近的时间轴更新
EdwardEan

EdwardEan

🏢  世界邦旅行网 / iOS攻城狮
V2EX 第 30591 号会员,加入于 2012-12-13 19:40:04 +08:00
EdwardEan 最近回复了
2016-04-12 14:29:43 +08:00
回复了 Dean 创建的主题 iDev iOS GCD 线程死锁的疑问
接上一段回复内容:

dispatch_sync(dispatch_get_global_queue(0, 0), ^{

[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"First");
}];

[[NSOperationQueue currentQueue] addOperationWithBlock:^{
NSLog(@"Three");
}];

__block id observer = [[NSNotificationCenter defaultCenter] addObserverForName:@"MyNotif" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
NSLog(@"Receive Notif");
[[NSNotificationCenter defaultCenter] removeObserver:observer];
}];

[[NSOperationQueue currentQueue] addOperationWithBlock:^{
NSLog(@"Forth");
}];

[[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotif" object:self];

[[NSOperationQueue currentQueue] addOperationWithBlock:^{
NSLog(@"Five");
}];
});
2016-04-12 14:28:51 +08:00
回复了 Dean 创建的主题 iDev iOS GCD 线程死锁的疑问
不知道题主有没有读过 dispatch_sync 方法的这一段注释:
As an optimization, dispatch_sync() invokes the block on the current thread when possible.
如果明白了请试着考虑下下面一段代码的输出结果:
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   914 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 66ms · UTC 21:24 · PVG 05:24 · LAX 13:24 · JFK 16:24
Developed with CodeLauncher
♥ Do have faith in what you're doing.