在快速实践中,我编写了名为OrderedSet
的简单结构。
我尝试OrderedSet
在GCD串行队列中是线程安全的。
但这不起作用。测试结果不稳定。我期望这样的事情:
20:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
但收到类似
2:[3, 19]
这是游乐场代码:
import Foundation
import XCPlayground
struct OrderedSet<T: Equatable> {
mutating func append(e: T) {
dispatch_sync(q) {
if !self.__elements.contains(e) {
self.__elements.append(e)
}
}
}
var elements: [T] {
var elements: [T] = []
dispatch_sync(q) {
elements = self.__elements
}
return elements
}
var count: Int {
var ret = 0
dispatch_sync(q) {
ret = self.__elements.count
}
return ret
}
private var __elements: [T] = []
private let q = dispatch_queue_create("OrderedSet.private.serial.queue", DISPATCH_QUEUE_SERIAL)
}
extension OrderedSet: CustomStringConvertible {
var description: String {
var text = ""
dispatch_sync(q) {
text = "\(self.__elements.count):\(self.__elements)"
}
return text
}
}
// Test code
let globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
let group = dispatch_group_create()
var testSet = OrderedSet<Int>()
for i in 0..<20 {
dispatch_group_async(group, globalQueue) {
testSet.append(i)
}
}
dispatch_group_notify(group, globalQueue) {
print("\(testSet)") // unstable result
}
XCPSetExecutionShouldContinueIndefinitely()
我已经检查了以下内容:
如果将
OrderdSet
定义为类(而非结构),就可以了。如果使用信号量而不是串行队列就可以了。
我想知道结构和串行队列对不稳定的原因。
- - 更新
我得到了这些预期的结果。
import Foundation
import XCPlayground
class OrderedSet<T: Equatable> {
func append(e: T) {
dispatch_sync(q) {
if !self.__elements.contains(e) {
self.__elements.append(e)
}
}
}
var elements: [T] {
var elements: [T] = []
dispatch_sync(q) {
elements = self.__elements
}
return elements
}
var count: Int {
var ret = 0
dispatch_sync(q) {
ret = self.__elements.count
}
return ret
}
private var __elements: [T] = []
private let q = dispatch_queue_create("OrderedSet.private.serial.queue", DISPATCH_QUEUE_SERIAL)
}
extension OrderedSet: CustomStringConvertible {
var description: String {
var text = ""
dispatch_sync(q) {
text = "\(self.__elements.count):\(self.__elements)"
}
return text
}
}
// Test code
let globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
let group = dispatch_group_create()
var testSet = OrderedSet<Int>()
for i in 0..<20 {
dispatch_group_async(group, globalQueue) {
testSet.append(i)
}
}
dispatch_group_notify(group, globalQueue) {
print("\(testSet)") // It's OK
}
XCPSetExecutionShouldContinueIndefinitely()
import Foundation
import XCPlayground
struct OrderedSet<T: Equatable> {
mutating func append(e: T) {
dispatch_semaphore_wait(s, DISPATCH_TIME_FOREVER)
if !self.__elements.contains(e) {
self.__elements.append(e)
}
dispatch_semaphore_signal(s)
}
var elements: [T] {
var elements: [T] = []
dispatch_semaphore_wait(s, DISPATCH_TIME_FOREVER)
elements = self.__elements
dispatch_semaphore_signal(s)
return elements
}
var count: Int {
var ret = 0
dispatch_semaphore_wait(s, DISPATCH_TIME_FOREVER)
ret = self.__elements.count
dispatch_semaphore_signal(s)
return ret
}
private var __elements: [T] = []
private let s = dispatch_semaphore_create(1)
}
extension OrderedSet: CustomStringConvertible {
var description: String {
var text = ""
dispatch_semaphore_wait(s, DISPATCH_TIME_FOREVER)
text = "\(self.__elements.count):\(self.__elements)"
dispatch_semaphore_signal(s)
return text
}
}
// Test code
let globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
let group = dispatch_group_create()
var testSet = OrderedSet<Int>()
for i in 0..<20 {
dispatch_group_async(group, globalQueue) {
testSet.append(i)
}
}
dispatch_group_notify(group, globalQueue) {
print("\(testSet)") // It's OK
}
XCPSetExecutionShouldContinueIndefinitely()
import Foundation
import XCPlayground
struct OrderedSet<T: Equatable> {
mutating func append(e: T) {
if !self.__elements.contains(e) {
self.__elements.append(e)
}
}
var elements: [T] {
return self.__elements
}
var count: Int {
return self.__elements.count
}
private var __elements: [T] = []
}
extension OrderedSet: CustomStringConvertible {
var description: String {
return "\(self.__elements.count):\(self.__elements)"
}
}
// Test code
let globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
let serialQueue = dispatch_queue_create("serial", DISPATCH_QUEUE_SERIAL)
let group = dispatch_group_create()
var testSet = OrderedSet<Int>()
for i in 0..<20 {
dispatch_group_async(group, globalQueue) {
dispatch_sync(serialQueue) {
testSet.append(i)
}
}
}
dispatch_group_notify(group, serialQueue) {
print("\(testSet)") // It's OK
}
XCPSetExecutionShouldContinueIndefinitely()
最佳答案
此代码将捕获testSet
的当前值:
dispatch_group_async(group, globalQueue) {
testSet.append(i) // `testSet` inside the closure will be a copy of the `testSet` variable outside
}
执行完关闭操作后,内部
testSet
的值将被复制到外部testSet
变量。想象一个并发的世界:
testSet
的初始值为“0:[]”。 testSet
副本尝试将其复制回唯一的外部testSet
。但是,只有一个获胜者,例如,外部testSet
的当前值为“1:[3]”。 testSet
的当前值为“1:[3]”,附加i
并复制回去,产生奇怪的结果,例如“2:[3,19]” 在更新后的案例1中,将
OrderedSet
更改为class,事情非常简单,testSet
被引用捕获,并且所有线程都共享同一对象。在您更新的案例3中,通过使用串行队列,我想每个附加和复制回操作都是串行的,因此您会产生一个完美的有序集。
情况2更复杂。其实我还没有弄清楚到底是怎么回事。而且我认为这更多是关于swift编译器的实现细节,并且可能会在不同的swift版本上进行更改。信号量似乎是一种引用类型,因此'testSet'的所有副本都共享相同的信号量。我猜编译器决定在这种情况下进行一些优化,并将
testSet
的__element
的所有副本都指向同一数组。因此,结果包含所有0 ..关于ios - Swift 2 : struct thread-safety,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31626004/