V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
V2EX  ›  EdwardEan  ›  全部回复第 1 页 / 共 1 页
回复总数  2
2016 年 4 月 12 日
回复了 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 年 4 月 12 日
回复了 Dean 创建的主题 iDev iOS GCD 线程死锁的疑问
不知道题主有没有读过 dispatch_sync 方法的这一段注释:
As an optimization, dispatch_sync() invokes the block on the current thread when possible.
如果明白了请试着考虑下下面一段代码的输出结果:
About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3655 Online   Highest 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 12ms · UTC 00:42 · PVG 08:42 · LAX 17:42 · JFK 20:42
♥ Do have faith in what you're doing.