Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
並行処理(超入門)
Search
ryo takaya
May 24, 2023
0
11
並行処理(超入門)
ryo takaya
May 24, 2023
Tweet
Share
More Decks by ryo takaya
See All by ryo takaya
軽量なスレッドとは?
ryotakaya
0
100
PHP8.0へのバージョンアップ始めました
ryotakaya
0
680
Featured
See All Featured
A Modern Web Designer's Workflow
chriscoyier
694
190k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
44
2.4k
Optimizing for Happiness
mojombo
379
70k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Done Done
chrislema
184
16k
Designing for Performance
lara
610
69k
How to Ace a Technical Interview
jacobian
277
23k
GraphQLとの向き合い方2022年版
quramy
49
14k
Optimising Largest Contentful Paint
csswizardry
37
3.3k
KATA
mclloyd
30
14k
The Cost Of JavaScript in 2023
addyosmani
51
8.5k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
1.8k
Transcript
並行処理(超入門)
並行処理をしたら何が嬉しいのか 複数の処理を分散並行して行う事によって処理時間が短縮する - DBの読み込みの性能を上げる工夫(indexとか) - http通信でユーザーへのレスポンスを短くするための工夫(CDNとか) 基本的に速さは正義
並行処理と並列処理の違い 時間軸の違い • 並行処理: ある時間の範囲において、複数のタスクを扱うこと • 並列処理: ある時間の点において、複数のタスクを扱うこと 必ずしも同時に実行されているわけではない
並行処理は考えなきゃいけないポイントが多々ある - その処理は並列化をしたら早くなるのか?? - 必ず早くなるとは限らない - (ex. 順序に意味のある処理 - アムダールの法則
- 競合状態 - 共通のメモリにアクセス - データ競合、ロック
goroutine - プログラムを動作させる基本の単位 - goキーワードを記述するだけ
- fork-joinモデル - プログラムの処理を分岐させて親と子で並行に実行する - fork : goroutineを発生させて分岐する - join
: 分岐元の親のgoroutineと合流する 分岐元のgoroutineが先に終了した場合、分岐先のgoroutineが実行されず終了する
sync.WaitGroup 1. wg.Add()でカウンタを増やす 2.wg.Done()でカウンタを減らす 3.wg.Wait()で内部のカウンタが 0になるまでブロックする Wait()が合流ポイントとなっているため分岐先のgoroutineが実行が終わるまで分岐元 のgoroutineが終了しない
- Mutex - Locker - Cond https://pkg.go.dev/sync
channel ゴールーチン間で値を受け渡す際に使用する
- 値を送信・受信するまでブロックする - 3秒たってchannelに値を送信。その後分岐元の goroutineでchannelから値を受け取るまでブロック する。 channelは同期の手段でもある
None