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
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
Transcript
21 slides
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で再実 装すればいい
まとめ スレッドはつくれる
More Decks by Shugo Maeda
See All by Shugo Maeda
Refining Active Record
shugo
0
560
Refining refinements
shugo
1
340
Other Decks in Programming
See All in Programming
なぜ私たちは「現時点では」KMPの導入を見送ったのか
chigichan24
1
640
正解の無い問題を 「できるだけ上手く」解く ヒューリスティックで感性を磨こう。〜現実課題をプログラミングで解きあうコミュニティについて〜
allegrogiken
0
120
AI Augmentation Techniques with Liberty
ivargrimstad
1
580
エラーチャンネルのボールを拾い続けて鍛えるエンジニアの地力 / phpcon-niigata2026
meihei3
1
510
Kiro Crewによる開発体験ーKiro CrewでStrands Decider 2B に関する技術検証をやってみた
satoshi256kbyte
1
160
느린 네트워크와 저사양 디바이스 사용자를 위한 웹뷰 로딩 시간 개선기
kmkim
1
270
[DroidKnights 2026] Workflow AX: From Auto-Documented Policies to E2E Testing
raeminkang
0
610
AIが生成したコードは読むべきか?抽象化設計とコードの品質保証 / abstraction-design-and-quality
minodriven
17
9.6k
How SRE Grew Out of Server-Side Work and Became the Team's Own / サーバサイドから始まったSREを チームに渡すまで
maaaato
1
260
JavaScriptはシングルバイナリの夢を見るか / Does JavaScript Dream of a Single Binary?
ssssota
0
420
Going Full Circle with Virtual Graphs and Relational Databases
michaelsimons
1
120
Spec-driven Development with AI
simas
PRO
0
2.3k
Featured
See All Featured
Ethics towards AI in product and experience design
skipperchong
2
470
30 Presentation Tips
portentint
PRO
1
480
Building an army of robots
kneath
307
47k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.7k
Tell your own story through comics
letsgokoyo
1
1.1k
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
2
2.1k
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
650
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
63
48k
Chasing Engaging Ingredients in Design
codingconduct
0
410
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.7k
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
1.5k
A Tale of Four Properties
chriscoyier
163
24k