Help us understand the problem. What is going on with this article?

ざっくりと理解するSwift同期処理

概要

同期処理を実装する機会があったのでまとめた
syncとasyncの二種類ある
(7/1 syncの部分がasyncになっていたので修正しました)

async: 他のスレッド処理終了を待機しない

async
DispatchQueue.global().async {
  print("global thread start.")
  //何か重い処理
  print("global thread end.")
}
DispatchQueue.global().async {
  print("main thread start.")
  //何か重い処理
  print("main thread end.")
}

# result
global thread start.
main thread start.
global thread end.
main thread end.
async
start==(Thread 1)==>end  
start==(Thread 2)=====>end

いわゆる並列処理
重い処理の裏で別処理を動かしたいときなどに使える

sync: 他のスレッド処理終了を待機する

sync
DispatchQueue.global().async {
  print("global thread start.")
  //何か重い処理
  print("global thread end.")
}
DispatchQueue.global().sync {
  print("main thread start.")
  //何か重い処理
  print("main thread end.")
}

# result
global thread start.
global thread end.
main thread start.
main thread end.
sync
start==(Thread 1)==>end  start==(Thread 2)====>end

いわゆる直列処理
クリティカルセクションなどに使える

shiba1208
新卒フルスタックエンジニア
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした