- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
鉴于以下代码,是否不需要 @synchronized
调用?
我假设 dispatch_group_enter/leave
是原子的,但这里没有将其列为线程安全 https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/dispatch_group_leave.3.html我突然担心我一直都搞砸了。
// Imagine this is on its own queue already (possibly main, possibly not)
dispatch_group_t group = dispatch_group_create();
for(x in array){
dispatch_group_enter(group);
[x doSomethingAsync:^{
// imagine x is part of a library which sometimes runs blocks on
// a different queue.
// surely dispatch_group_leave has its own internal synchronization?
@synchronized(group){
dispatch_group_leave(group);
}
}]
}
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
最佳答案
调用 dispatch_group_enter
和 dispatch_group_leave
时不需要提供任何额外的同步。 GCD 本身提供并发和线程工具。如果需要额外的同步或障碍,我的期望是它会在文档中被明确调用,就像调用 dispatch_once_t
具有全局/静态存储的要求一样。换句话说,GCD 是一个线程库,因此除非另有说明,否则预期它的调用应该提供彼此相关的线程安全。
这种期望是基于很多个人经验和 Greg Parker 制作 a similar statement on a mailing list不久前。
关于ios - 我应该将dispatch_group_leave包装在@synchronized block 中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30476965/
我知道 dispatch_group_leave 调用需要与 dispatch_group_enter 平衡。这是发生崩溃的代码块(随机发生,并非总是如此) dispatch_
这种情况很少发生。这是堆栈跟踪的最后一行: 0 libdispatch.dylib 0x0000000197a85a9c dispatch_group_leave + 48
我正在使用分派(dispatch)组来获取数据字符串。这是一段代码,我不确定为什么会在这段代码中得到 Unbalanced call to dispatch_group_leave()。 var qu
我正在尝试使用以下代码向我在 firebase 中的节点添加一个新子节点: @IBAction func likeButtonOnTouch(_ sender: Any) { if ViewC
在我的 dispatch_group 代码中,我使用 dispatch_group_wait 使一组网络服务调用超时。问题,似乎我需要计算调用 dispatch_group_enter 的次数,然后调
我有下面这个逻辑;它们基本上是 3 个嵌套的调度组 block 。 第一组(组)将执行 3 个简短的异步任务(仅从网络服务下载数据)和一个较长的异步任务:将未同步的记录上传到网络服务,在本地删除同步的
我是一名优秀的程序员,十分优秀!