Rey*_*lar 13 multithreading swift
我有以下代码:
func foo() {
var sum = 0
var pendingElements = 10
for i in 0 ..< 10 {
proccessElementAsync(i) { value in
sum += value
pendingElements--
if pendingElements == 0 {
println(sum)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,函数proccessElementAsync,如其名称所示,异步处理其输入参数,并在完成时调用其相应的完成处理程序.
这种方法的不便之处在于,由于变量pendingElements是通过多个线程访问的,因此该语句if pendingElements == 0可能永远不会为true.
在C#中,我们可以做类似的事情:
Object lockObject = new Object();
...
lock (lockObject) {
pendingElements--;
if (pendingElements == 0) {
Console.WriteLine(sum);
}
}
Run Code Online (Sandbox Code Playgroud)
这确保了只能为一个线程同时访问此变量.有没有办法在Swift中获得相同的行为?
小智 16
希望这会帮助你.
func lock(obj: AnyObject, blk:() -> ()) {
objc_sync_enter(obj)
blk()
objc_sync_exit(obj)
}
var pendingElements = 10
func foo() {
var sum = 0
var pendingElements = 10
for i in 0 ..< 10 {
proccessElementAsync(i) { value in
lock(pendingElements) {
sum += value
pendingElements--
if pendingElements == 0 {
println(sum)
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
没有本机锁定工具,但有一些解决方法,如这个 SO 问题中所述:
Swift 相当于 Objective-C 的“@synchronized”是什么?
使用其中一个答案,您可以创建一个函数:
func synchronize(lockObj: AnyObject!, closure: ()->()){
objc_sync_enter(lockObj)
closure()
objc_sync_exit(lockObj)
}
Run Code Online (Sandbox Code Playgroud)
进而:
func foo() {
var sum = 0
var pendingElements = 10
for i in 0 ..< 10 {
processElementAsync(i) { value in
synchronize(pendingElements) {
sum += value
pendingElements--
if pendingElements == 0 {
println(sum)
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)