dispatch_semaphore_createを使った別スレッドの処理を待つ書き方

同期処理というのかな?並列処理とか苦手だなぁ。

print("hoge-1")
let semaphore = dispatch_semaphore_create(0)
print("hoge-2")
let queue = dispatch_get_main_queue()
print("hoge-3")
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
    print("hoge-4")
    sleep(10)
    print("hoge-5")
    dispatch_semaphore_signal(semaphore)
    print("hoge-6")
})
print("moge-1")
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)
print("moge-2")

結果は

hoge-1
hoge-2
hoge-3
hoge-4
moge-1
hoge-5
hoge-6
moge-2

iOS - 非同期処理dispatch_queueのまとめ - Kamanii Square