は私がデフォルトディスパッチキューは、OS_dispatch_queue_serial呼ばシリアルキューように見えることに注意してください。スイフト3:OS_dispatch_queue_serialがデフォルトのキューになっているのはなぜですか?私は、次の遊び場を実行すると
なぜですか?これに関連して
この場合には、彼らは両方の同期およびシリアルに行動しているように見えるように私は、非同期と同期の意味用法に混乱しています。
私は間違っていますか? シリアルと同期を混同していますか?私は次のことを達成するために期待していたコードを実行する前に
:
Hello World
Hello Moon
world * (or Moon)
Moon * (or world)
// Note: here I assumed that async meant that the codes in the block would have been run in parallel to each other and that this would have meant that either world or Moon would have been printed depending on **race conditions**
Hello World
world
Hello Moon
Moon
遊び場コード:
import Foundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
let queue = DispatchQueue(label: "com.test.myqueue")
queue.async {
print("Hello World")
sleep(2)
print("world")
}
queue.async {
print("Hello Moon")
sleep(1)
print("Moon")
}
queue.sync {
print("Hello World")
sleep(2)
print("world")
}
queue.sync {
print("Hello Moon")
sleep(1)
print("Moon")
}
この迅速を理解するすべてのヘルプは素晴らしいことです。
もともと(のiOS 4/OS X 10.6)あなたがGCDで同時キューを作成できませんでした。システム並行キューがありましたが、それ以上は作成できませんでした。 iOS 4.3/OS X 10.7では、並行キューを作成する機能を追加しましたが、シリアルキューはデフォルトとして保持していました。実際には、通常、プロデューサ/コンシューマパターン(https://github.com/iosptl/ios7ptl/blob/master/ch23-AdvGCD/ProducerConsumer/ViewController.m)を実装するときに、カスタムの並行キューはほとんど役に立ちません。 。通常、システム1で十分です。通常、カスタムキューを作成すると、アクセスがシリアライズされます。 –
(私が列挙した例でも、実際にはカスタムの並行キューは必要ありませんでしたが、それはシステムキューの1つでうまくいきましたので、それはまれです...:Dしかし、 –