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
More Decks by Shugo Maeda
See All by Shugo Maeda
Refining Active Record
shugo
0
110
Refining refinements
shugo
1
330
Other Decks in Programming
See All in Programming
AI が書く Go コードの品質を劇的に向上させる Linter: “declscope”
mpyw
0
490
Androidだけじゃない、DroidKaigiで広がった私の技術スタック
akkeylab
0
130
Agentic Software Factoryに、すごく賢いIF文を。 / super smart IF statement into the Agentic Software Factory.
rkaga
8
2.2k
AI主導のプログラミング学習アプリを作ってみた
econanringo
0
170
ChromeでローカルLLM
ryu1013
0
130
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
660
コーディング自動化の次へ。人の介入をゼロにするファクトリーとサポートAIによる実践
timakin
2
1.8k
個人開発者よ、Honoを使え(おじさんver)
tanahiro2010
2
250
JRuby: Past, Present, and Future
headius
0
230
Kotlinの15年とServer-Side Kotlin
n_takehata
1
140
複雑なUIコンポーネントとの ⻑い付き合い⽅:波及するバグからテスト戦略を経て、安全な再設計へ
mixplace
0
390
Unity6 URPによる海底をテーマにしたVJシステムの解説 | Unity Shader 完全に理解した 勉強会
gam0022
0
360
Featured
See All Featured
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
63
46k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
230
23k
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
560
ラッコキーワード サービス紹介資料
rakko
1
5.3M
Rebuilding a faster, lazier Slack
samanthasiow
85
9.7k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.9k
Building a Scalable Design System with Sketch
lauravandoore
464
34k
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
630
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
360
The #1 spot is gone: here's how to win anyway
tamaranovitovic
4
1.2k
Building a A Zero-Code AI SEO Workflow
portentint
PRO
0
780
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
460
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で再実 装すればいい
まとめ スレッドはつくれる