Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
go
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
xorphitus
February 04, 2015
Programming
310
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
go
go な並行処理の中身を紐解いてみました。
(Golang, clojure.core.async)
xorphitus
February 04, 2015
More Decks by xorphitus
See All by xorphitus
オリジナリティのあるGitLabを標準に近づける
xorphitus
1
800
マイクロサービスを作ろう
xorphitus
0
150
コンテナ起動への道
xorphitus
0
170
型システムを学ぼうとした結果
xorphitus
0
82
M-x doctor
xorphitus
0
180
型で数を表そう
xorphitus
0
110
AOT と direct linking
xorphitus
0
91
CFS入門
xorphitus
0
91
HyperLogLog
xorphitus
0
130
Other Decks in Programming
See All in Programming
MVNOの申込からeSIM開通までをiOSアプリでつなぐ- 本人確認・MNP・通信事業者基盤をまたぐ実装
satotakeshi
0
480
Intent as Code
shoppingjaws
6
1.1k
ソニーのクラウド共通基盤の変遷とAI時代の開発スタイルに合わせた進化 / The Journey of Sony’s Common Cloud Platform and Its Evolution for AI-Native Development
kenjiyoneyama
0
110
世界の中心で、AI(App Intents)をさけぶ ー App Intents中心設計の実践ガイド
touyou
0
690
Building an Out-of-Order CPU
latte72
1
800
iOSDC Japan 2026 - Swiftで作って学ぼう!データベース自作入門
kaseken
2
350
AGENTS.md Is Not Enough:Build Skills, Don't Download Them
lx_t
0
130
個人開発基盤をまるごとCloudflareに引っ越して爆速で総合的体験を向上させた話
tinykitten
0
210
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
300
wkhtmltopdfの次どうするか問題2026
willnet
2
1.8k
Snowflakeで業務アプリを作ろう。 Snowflakeのアプリ機能解説&実践ガイド
ayumu_yamaguchi
2
300
速習iPhone Duo対応
yuukiw00w
2
830
Featured
See All Featured
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
2
920
Principles of Awesome APIs and How to Build Them.
keavy
128
18k
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
Chasing Engaging Ingredients in Design
codingconduct
0
340
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
63
46k
Agile Leadership in an Agile Organization
kimpetersen
PRO
0
250
Tell your own story through comics
letsgokoyo
1
1.1k
Documentation Writing (for coders)
carmenintech
77
5.5k
Skip the Path - Find Your Career Trail
mkilby
1
240
Navigating Team Friction
lara
192
16k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
203
76k
Become a Pro
speakerdeck
PRO
31
6.3k
Transcript
go @xorphitus (2015-02)
この動物は並行処理が得意らしい
それを、こうじゃ ( )
S式 ( ) go
(let [ch (chan)] (go (while true (let [val (<! ch)]
(println val)))) (go (>! ch “hello world”)))
go は大流行のようです
Go
今日は go な並行処理の 内側を少し覗いてみます
※ 書き方、使い方の話はしない
並行処理の俯瞰図(たぶん) 協調マルチタスク Python、Lua、あたりが主? ECMA6 でもやろうと思えば 共有メモリ ロック 例のあれ STM ClojureやらHaskellやら
メッセージ パッシング アクター Scalaやら Erlangやら CSP/π計算 ベース Goやら Clojureのcore.asyncやら C# async やら
Go の並行処理 (goroutine) CSP (Communicating Sequential Processes) および π計算 (pi-calculus)
の2つを混ぜて作った並行処理、らしい チャネル渡しの概念はπ計算のもの、らしい
CSP と π計算 どちらもプロセス代数の一種 プロセス代数っていうのは • 並行システムを代数で表現したもの • メッセージパッシング •
代数式を解くことで並行処理の恒等的性質を明 らかにする、らしい ◦ これ、デッドロックするんじゃね?とか
CSP 自販機の例 自販機はコインを入れてジュースが出て止まる VendingMachne = coin -> juice -> STOP
人間はコインまたはカードでの支払いを選択し、止まる Person = (coin -> STOP)□(card -> STOP) 自販機と人間を並列化すると VendingMachine|[{coin, card}]|Person ≡ coin -> juice -> STOP 上記を何やかんやすると、自販機・人間の並列プロセスから (juice -> STOP)□STOP ジュースを出して止まる、または単に止まる、非決定性プロセスが得られる
「らしい」
None
商業プログラマから見た goroutine 自分のような商業プログラマには とにかく軽量なスレッド として有り難く使わせてもらえれば十分 背後の計算機科学的理論は、2分で挫折
まさに商業プログラマ!!
ところで 軽量スレッドって 何だろう?
軽量スレッド/文法的なところは • メッセージパッシングにより ◦ レースコンディションの回避 ◦ ロックをとってほげほげ、の回避 • メッセージの授受や sleep
等のイベント処理 が、call back hell なしに書ける
軽量スレッド/もう少し低レイヤ • OSのスレッドよりも更に軽い • 一度に立ち上げられる数がたくさん • コンテキストスイッチのコストが低い • 単一のスレッド内でも複数起動し得る •
ファイバはこれに近いっちゃ近いけど軽量スレッ ドはプリエンプティブ(に見える) • アクターも同じノリだろうか?
何これ どうやって動いてんの?
Go のアプローチ goroutine とは 「スレッドによって多重化されたコルーチン」 らしい 次スライドにイメージ図を描いてみた 参考 http://golang.org/doc/faq#goroutines http://stackoverflow.com/questions/24599645/
goroutine のイメージ(たぶん) coroutine 0 thread 0 coroutine 1 blocking thread
1 coroutine 1 scheduler call call yield 待機状態の coroutineを 移動! 別スレッドにて coroutine 0 を 待たずに続行
goroutine マルチコア対応 環境変数 GOMAXPROCS を変更する この値に応じて実スレッドを起動し goroutine の処理を分散して割り当てる デフォルトは 1
Clojure のアプローチ go はというのはマクロで 中身を停止・再開可能なステートマシンに 変換して 他のタスクにブロックされている間は止める 参考 http://clojure.com/blog/2013/06/28/clojure-core-async-channels.html http://endot.org/notes/2014-02-14-core-async-clojure/
go (Clojure) のイメージ (go (...) (...)) macro expansion (go (...)
(...)) macro expansion thread task 0 task 1 チャネルへの 入力待ち チャネルに 入力 IOC スレッドと言う
go (Clojure) のイメージ thread チャネルへの 入力待ち チャネルに 入力 state 待ちになったら
state を他所で維持しつつ スレッド上の処理から 除外する チャネルへの入力が あったら 元の状態にリジュームして 処理を継続する
go (Clojure) マルチコア対応 実際は、各ステートマシンの処理が 固定長スレッドプール上のスレッドに 割当てられることになる プールサイズはコア数に比例 2 * cores
+ 42
なるほど、軽量スレッド
OSおよび軽量スレッド/イメージ図 multi thread (OS) lightweight thread lightweight thread for multi
core サーバアーキテクチャで、まずイベント駆動が流行して その後コア数に合わせてイベント駆動プロセスを立ち上げるのが出てきた経緯に似てるなあ
※CPUバウンド
IOバウンドな処理だと • OSのスレッドを使った方がよい • 軽量スレッドだと処理がブロックされる • IO waitのコストがコンテキストスイッチのコスト を上回る •
ので、多量に並列するならスレッド上限が多く なっていることは確認しておく core.async なら thread マクロでそれでき(ry
まとめ • go な並行処理はスッキリ記述できて嬉しい • CPUリソースも無駄なく使えて嬉しい • ただしマルチコアによる並「列」化は意識してお く •
そしてIOバウンドな処理にも気をつける • あと、go マクロでステート・マシンに展開すると かヤバい