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
Haskellで並行プログラミングの練習
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Tomohiko Himura
May 08, 2016
Programming
340
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Haskellで並行プログラミングの練習
LT駆動25
https://github.com/LTDD/Sessions/wiki/LT%E9%A7%86%E5%8B%95%E9%96%8B%E7%99%BA25
Tomohiko Himura
May 08, 2016
Transcript
54 slides
HaskellͰ ฒߦϓϩάϥϛϯάͷ࿅श 2016-05-07 LTۦಈ։ൃ25
ͻΉΒ ͱͻ͜ ฒߦ ฒྻ ࢄ ͲΕ͍͠
None
None
言葉にすれば 世界は変えられる
ฒߦϓϩάϥϛϯά Λ͡ΊͯΈΔ
なぜ並行プログラミング
CPUはもう速くならないらしい
速くならないけど 幅は広くなるらしい
コアの数は増える
複数のコアを有効に使わなければ プログラムは速くならない
複数のコアを使って 別々のタスクを実行する
ノンブロッキングI/Oの登場で 効果がでやすい(要出典)
コアの複数化で 効果がでやすい(要出典)
並列処理より入門しやすい
並行処理といえば Go言語らしい
真似をするところから はじめよう
IUUQRJJUBDPNTVJOJUFNTFDCG⒎EGE
None
1秒かかる処理 2秒かかる処理 3秒かかる処理
Go言語では Goルーチンとチャンネル で並行化
1秒かかる処理 2秒かかる処理 3秒かかる処理 終わるの待つ
Goルーチン • 軽量スレッド • 数kbしかつかわない • javaのスレッドは320kbとか使うらしい • 関数呼び出しに goってつけるだけ
たとえば javaで • 1クライアント 1スレッド割り当てる • サーバープロセスを考える • 320kb *
10,000 -> 約3GB
Goルーチンなら いっぱいつくれる
チャネル • Goルーチンがやり取りするのに利用 • メッセージパッシング • 共有メモリではなく • メッセージが入ってくるまで待つ
None
Αʔ͠ HaskellͰͬͯΈΑ͏
None
None
threadDelay マイクロ秒待てる
6秒かかる
(P )BTLFMM ฒߦԽ HP͚ͬͯͭΔ GPSL*0͚ͬͯͭΔ ௨৴ νϟωϧΛ͔ͭ͏ .7BSΛ͔ͭ͏
\
None
> だいたい同じ <
forkIO :: IO () -> IO ThreadId 第1引数を別のスレッドで実行
MVar 値が入ってくる前に 読み込みしようとすると 値が入ってくるまで待つ
予定どおり3秒で実行できた
ॻ͖ࠐΈΛ ผεϨουʹͯ͠ΈΔ
同時に実行すると出力が混ざる
軽量スレッドは プリエンプティブ 短い時間でスレッドが切り替わっている
出力は同一スレッドで やってみる
Control.Concurrent.Chan MVarみたいな無限キュー
Endがやってくるまで Chan を読み続けて 画面に表示 Endがやってきたら finished(MVar)に値を書き込み スレッドの終了を通知
ChanはMVarをつかって 実装されている
軽量スレッドとMVar が並行処理の基本単位
綺麗に表示された
めでたしめでたし
ファイルへの出力より ログの生成が早いと メモリがあふれるので注意
·ͱΊ
並行処理 • Communicating Sequential Processes • ってのが元になっている模様 • erlangやakka •
アクターモデル • そのまま分散できる • どちらもメッセージパッシング
CSP • いままでどおりかけて見通しがよい • 結構いろんな言語で使える模様
まとめ • HaskellでGoのコードは真似られそう • しかし、全く同じように動くわけではない • 正しく違いを理解しないといけない • HaskellはIOが型で分離される •
より良いコードがかけるはず
More Decks by Tomohiko Himura
See All by Tomohiko Himura
Marpでmermaidは簡単だときいたけど
eiel
1
2.8k
バイナリ読むのにElixirしてみた
eiel
0
140
アジャイルはさておきMake People Awesomeしたい
eiel
0
240
レビューは最優先にするようにしている
eiel
0
390
再考 Fourkeys メトリクス
eiel
2
740
Test mockをSnapshot testする
eiel
0
200
devenvに入門した
eiel
1
230
関数プログラミングの考え方
eiel
1
410
逆コンウェイ作戦はフィードバックループを作るために 逆向きの流れをつくること (5分版)
eiel
0
570
Other Decks in Programming
See All in Programming
선언형 햅틱 라이브러리 진동을 소개합니다
l2hyunwoo
1
320
AWS CDKを理解するために公式の定義を深掘って解説してみる / Understanding AWS CDK by Digging into Its Official Definition
akihisaikeda
3
310
エージェントを使い続けるためのメタワーク / Meta-work for keeping using the agent
rkaga
6
2.3k
Vue.jsで作る空間解析アプリとCapacitorプラグインのData Bridge
hal_spidernight
1
180
高橋メソッドでCVE解説シリーズ 〜CVE-2026-112816〜
hakkadaikon
0
270
Goコンパイラを作ってGoをもっと理解する / Build a Go compiler to better understand Go
ikura_hamu
0
130
オーケストレーション記述言語 Katari
yukikurage
0
260
なぜ私たちは「現時点では」KMPの導入を見送ったのか
chigichan24
1
280
例外からRich Errorsまで Kotlinエラーハンドリングの進化を読み解く
shiita0903
0
200
Growing PicoRuby's Ecosystem on ESP32
yuuu
2
140
そのデザイントークン、Flutterでも同じ見た目になりますか?
nakamizu
0
370
SODA - FACT BOOK(EN)
sodainc
1
250
Featured
See All Featured
Being A Developer After 40
akosma
PRO
92
590k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
370
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
1k
We Have a Design System, Now What?
morganepeng
55
8.4k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
23k
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
Navigating Algorithm Shifts & AI Overviews - #SMXNext
aleyda
1
1.7k
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
121
130k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1.4k
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
480
So, you think you're a good person
axbom
PRO
2
2.3k
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.7k