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
Rubyでつくるスレッド
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Shugo Maeda
June 30, 2018
Programming
1.3k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Rubyでつくるスレッド
松江Ruby会議09のLTで、callccを使ってスレッドを実装する話をしました。
Shugo Maeda
June 30, 2018
More Decks by Shugo Maeda
See All by Shugo Maeda
Refining refinements
shugo
1
330
Other Decks in Programming
See All in Programming
[2026-09-26]空論ジェネリックプロセス~テスト資産とAIで紡ぐ、再現可能なパフォーマンスチューニングの話~
tosite
0
220
WebRTC映像をAirPlayに対応させる挑戦.pdf
monolithic_adam
0
320
AI Agent時代のリアーキテクチャ戦略と実践
hokaccha
9
5.1k
Workers Cache を知る
syumai
0
300
SREの越境 / SRE Collaboration
y0hgi
2
290
Are APIs Still Relevant in the AI Era?
soyuka
0
330
Apple Intelligence を用いた個人情報誤送信防止、及びユーザーリクエスト体験の改善について
yukiny
0
190
半永久的に提供し続けられるプライベートクラウドを目指して ― 利用者の認知負荷を抑えるAPI抽象化とハードウェア世代交代の基盤設計
tomokon
0
150
iOS 27でニュースアプリはどう変わる!? 〜日経電子版の新機能対応と、開発事例から〜
lynnswap
7
13k
AgentCore CLI で進化した AWS での AI エージェントの作り方 : 必要な機能を必要な時に
icoxfog417
PRO
3
390
速習iPhone Duo対応
yuukiw00w
2
850
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
390
Featured
See All Featured
Principles of Awesome APIs and How to Build Them.
keavy
128
18k
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
530
A Modern Web Designer's Workflow
chriscoyier
699
190k
Joys of Absence: A Defence of Solitary Play
codingconduct
1
530
Typedesign – Prime Four
hannesfritz
42
3.2k
エンジニアに許された特別な時間の終わり
watany
109
250k
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
310
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
710
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.8k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
Fashionably flexible responsive web design (full day workshop)
malarkey
409
67k
Transcript
Rubyでつくる スレッド Shugo Maeda NaCl 2018-06-30
やりたいこと MinThread.start do 20.times do |i| puts "Thread#1: #{i}" sleep(0.1)
end end MinThread.start do 20.times do |i| puts "Thread#2: #{i}" sleep(0.1) end end
継続(Continuation) 次に実行される計算を表す グローバルgoto オブジェクトの状態は戻らない Ruby 1.8のスレッドの実装を利用 [ruby-dev:4083] 継続でスレッドをつくれるのでは?
わかる人にはわかる説明(1) Ruby 1.8のスレッドはsetjmp()/ longjmp()で切り替える スタックは自前で保存して書き戻す 継続も同じ仕組み
わかる人にはわかる説明(2) スレッド 並行宇宙 継続 世界線
継続の例 require "continuation" callcc {|c| $cont = c} print "Hello,
World!\n" $cont.call
実装
スレッドの作成 module MinThread QUEUE = [] def self.start(&block) QUEUE.push(block) end
スレッドの実行 def self.resume proc = QUEUE.shift if proc proc.call end
end at_exit do MinThread.resume end
スレッドの切替 def self.pass callcc do |c| QUEUE.push(c) resume end end
動いた! MinThread.start do 20.times do |i| puts "Thread#1: #{i}" sleep(0.1)
MinThread.pass end end MinThread.start do 20.times do |i| puts "Thread#2: #{i}" sleep(0.1) MinThread.pass end end
でも何か違う MinThread.start do 20.times do |i| puts "Thread#1: #{i}" sleep(0.1)
MinThread.pass # これが必要 end end MinThread.start do 20.times do |i| puts "Thread#2: #{i}" sleep(0.1) MinThread.pass # これが必要 end end
勝手にスレッドを切り替えたい
TracePoint Ruby実行中のイベントをフック フックで切り替えればいいのでは?
実装 at_exit do MinThread.set_next_switch_time TracePoint.trace(:line) do |tp| MinThread.schedule # 一定時間毎にThread.pass
end MinThread.resume end
1回しか切り替わらない!
理由 フックの中ではTracePointが無効 化される フック中で継続を呼ぶと無効化され たまま
じゃあモンキーパッチで at_exit do MinThread.set_next_switch_time [Integer, String, Array, Hash, IO, File].each
do |mod| mod.prepend Module.new { mod.instance_methods(false).each do |method| define_method(method) do |*args, &block| MinThread.schedule super(*args, &block) end end } end MinThread.resume end
デモ
課題 IOなどでブロックすると全部止まる IO#readなどをノンブロッキングIOで再実 装すればいい
まとめ スレッドはつくれる