SlideShare una empresa de Scribd logo
1 de 45
Descargar para leer sin conexión
CONCURRENCY:
RUBIES, PLURAL
Elise Huard - Arrrrcamp 2010
Friday 29 October 2010
MULTIPROCESSOR/MULTICORE
Friday 29 October 2010
NETWORK ON CHIP
(50..96..100 CORES)
Friday 29 October 2010
“... for the first time in history, no one is building a
much faster sequential processor. If you want your
programs to run significantly faster (...) you’re going
to have to parallelize your program.”
Hennessy and Patterson “Computer Architectures” (4th
edition, 2007)
Friday 29 October 2010
But ...
forget all that
(mostly)
Friday 29 October 2010
Friday 29 October 2010
Friday 29 October 2010
language VM
OS
(kernel processes, other processes)
Your program
multicore - multiCPU
Friday 29 October 2010
Concurrent programs
!=
Parallel computing
Friday 29 October 2010
3 threads, 2 cores
1
2
3
1
1
2
3
1
2
3
core1 core2 core1 core2
Friday 29 October 2010
Scheduling
• preemptive -> thread is told to yield
(by kernel or other thread)
• cooperative -> thread yields control
Friday 29 October 2010
Processes,Threads
Process 2
RAMmemory space
Process 1
thread1
scheduler (OS)
CPU CPU
memory space
thread2 t1 t2 t3
Friday 29 October 2010
Ruby
• Process
• Thread:
• green threads in MRI 1.8
• native threads in MRI 1.9, Rubinius,
JRuby, ...
Friday 29 October 2010
MRI: GIL
from http://www.igvita.com/2008/11/13/concurrency-is-a-myth-in-ruby/ @igrigorik
Friday 29 October 2010
MRI: GIL
• only one thread is executed at a time
• fair scheduling: timer thread!
(10 μs for Linux, 10 ms for Windows)
• blocking region to allow limited
concurrency
Friday 29 October 2010
Other Rubies
• @evanphx working on removing the
GIL on Rubinius (Hydra branch)
• JRuby, IronRuby, MacRuby don’t have
GIL
Friday 29 October 2010
Multiprocess
•advantage: separate state
•disadvantage: overhead to
spawning + context
switching
Friday 29 October 2010
Ruby: multiprocess
•fork and IPC: IO.pipe,
Mmap, sockets ...
•DRb
•Queues
Friday 29 October 2010
def execute(&block)
rd, wr = IO.pipe # to retrieve results
pid = fork do
rd.close
result = block.call
wr.write result.to_json
wr.close
end
wr.close
sorted = JSON.parse(rd.read)
rd.close
Process.waitpid(pid)
sorted
end
Ruby: multiprocess
Friday 29 October 2010
Threads
Shared state: locking to avoid race
conditions
Mutex.synchronize do
...
end
ConditionVariable (semaphore)
MonitorMixin, Sync_m
Friday 29 October 2010
Ruby threads
require 'thread'
threads = []
account = UnsafeAccount.new
mutex = Mutex.new
10.times do
threads << Thread.new do
mutex.synchronize do
account.receive(10)
end
end
end
threads.each {|t| t.join }
puts account.balance
Friday 29 October 2010
Fibers
• cooperative scheduling
• coroutines
• for MRI: lightweight
• JRuby, Rubinius: Fiber mapped to
native thread
Friday 29 October 2010
Ruby: Coroutines
require 'fiber'
# coroutines
ary = []
f2 = nil
f1 = Fiber.new{
puts "please give your login"
login = f2.transfer
puts login
puts "give password"
pass = f2.transfer
puts pass
f2.transfer
f2.transfer('***** no cigar *****')
}
f2 = Fiber.new{
f1.transfer('johndoe')
f1.transfer('ultrasecret')
answer = f1.transfer
puts answer
}
f1.resume
vaguely inspired by http://sheddingbikes.com/posts/1287306747.html
output:
please give your login
johndoe
give password
ultrasecret
***** no cigar *****
Friday 29 October 2010
MVM
Rubinius (2008): no parallel execution
of threads in oneVM ... so let’s create
oneVM per native thread
vm = Rubinius::VM.spawn "blah", "-e", "puts 'hellon'"
Friday 29 October 2010
Friday 29 October 2010
Shared State:
will melt your brain
• non-determinism
• atomicity
• deadlock - livelock
• fairness/starvation
• race conditions
• locks: loss of performance
Friday 29 October 2010
Friday 29 October 2010
Actor model
•named actors: no shared state
•asynchronous message passing
(fire and forget)
Friday 29 October 2010
CSP
• member of family of Process Calculi
(mathematical theory)
• events, processes
• synchronous (rendez-vous) message passing
• named channels - dual to Actor model
Friday 29 October 2010
Concurrency oriented
languages
• Erlang (Actors)
• Go (CSP)
• Haskell (several)
• Scala (Actors)
• Clojure
• IO
Friday 29 October 2010
Ideas
• functional programming: side effect
free
- immutable data
• nothing shared (advantage: distributed
= local)
• message passing
Friday 29 October 2010
Erlang
• Actor model:Actors, asynchronous
message passing
• actors = “green processes”
• efficientVM (SMP enabled since R12B)
• high reliability
© ericsson 2007
Friday 29 October 2010
Erlang
pid = spawn(fun() ->sort(Self, List) end)
pmap_gather([]) ->
[];
pmap_gather([H|T]) ->
receive
{H, Ret} -> [Ret|pmap_gather(T)]
end;
Friday 29 October 2010
Rubinius:Actors
• actors in the language: threads with
inbox
• VM actors to communicate between
actors in differentVMs
Friday 29 October 2010
Ruby: Revactor
• erlang-like semantics: actor spawn/receive,
filter
• Fibers (so cooperative scheduling)
• Revactor::TCP for non-blocking network
access (1.9.2) (rev eventloop)
Friday 29 October 2010
Go
• Fairly low-level - fit for systems
programming (close to C)
• static typing
• goroutines: parallel execution - sort of
async lightweight thread
• channels !
Friday 29 October 2010
Go
ReplyChannel = make(chan []int)
(...)
request.data = less
request.replyChannel = ReplyChannel
go sort(&request) // async start parallel
execution
in sort:
request.replyChannel <- sorted
listener:
result := <-replyChannel
Friday 29 October 2010
Ruby: futures
Lazy.rb gem (@mentalguy)
puts "before first future"
future1 = Lazy::Future.new { fib(40) }
puts "before second future"
future2 = Lazy::Future.new { fib(40) }
puts "and now we're waiting for results ..."
puts future1
# maybe some more code here
puts future2
Friday 29 October 2010
Clojure
functional, Lisp-like, on JVM
concurrency: Software Transactional
Memory System:
• Refs = shared, and mutation within a
transaction (atomic, consistent, isolated) -
Multiversion Concurrency Control -
retried if conflict
• Agents: independent, asynchronous change
Friday 29 October 2010
Clojure
(import '(java.util.concurrent Executors))
(defn parallel-stm [start amount nthreads]
(let [balance (ref start)
pool (Executors/newFixedThreadPool nthreads)
tasks (map (fn [t]
(fn []
(dosync
(alter balance transfer amount))))
(range nthreads))]
(doseq [future (.invokeAll pool tasks)]
(.get future))
(.shutdown pool)
(deref balance)))
Friday 29 October 2010
Ruby: STM
• @mentalguy thought experiment
• @technomancy clojure-gem
Friday 29 October 2010
other ways to handle
concurrency
see http://moonbase.rydia.net/mental/
blog/programming/concurrency-five-
ways.html (@mentalguy)
Friday 29 October 2010
Kernel stuff
Some of these problems have
been solved before ...
Friday 29 October 2010
FUN :)
Friday 29 October 2010
References:
http://www.delicious.com/elisehuard/concurrency
Elise Huard
@elise_huard
http://jabberwocky.eu
Friday 29 October 2010

Más contenido relacionado

La actualidad más candente

Python utan-stodhjul-motorsag
Python utan-stodhjul-motorsagPython utan-stodhjul-motorsag
Python utan-stodhjul-motorsag
niklal
 

La actualidad más candente (19)

[COSCUP 2018] uTensor C++ Code Generator
[COSCUP 2018] uTensor C++ Code Generator[COSCUP 2018] uTensor C++ Code Generator
[COSCUP 2018] uTensor C++ Code Generator
 
Linux containers
Linux containersLinux containers
Linux containers
 
Kotlin - Coroutine
Kotlin - CoroutineKotlin - Coroutine
Kotlin - Coroutine
 
Stackless Python In Eve
Stackless Python In EveStackless Python In Eve
Stackless Python In Eve
 
Go on!
Go on!Go on!
Go on!
 
GopherCon Denver LT 2018
GopherCon Denver LT 2018GopherCon Denver LT 2018
GopherCon Denver LT 2018
 
Introduction to-linux
Introduction to-linuxIntroduction to-linux
Introduction to-linux
 
15 - Streams
15 - Streams15 - Streams
15 - Streams
 
Go serving: Building server app with go
Go serving: Building server app with goGo serving: Building server app with go
Go serving: Building server app with go
 
Make A Shoot ‘Em Up Game with Amethyst Framework
Make A Shoot ‘Em Up Game with Amethyst FrameworkMake A Shoot ‘Em Up Game with Amethyst Framework
Make A Shoot ‘Em Up Game with Amethyst Framework
 
Python utan-stodhjul-motorsag
Python utan-stodhjul-motorsagPython utan-stodhjul-motorsag
Python utan-stodhjul-motorsag
 
Zoo First Presentation
Zoo First PresentationZoo First Presentation
Zoo First Presentation
 
Concurrency in Golang
Concurrency in GolangConcurrency in Golang
Concurrency in Golang
 
Go concurrency
Go concurrencyGo concurrency
Go concurrency
 
PenTest using Python By Purna Chander
PenTest using Python By Purna ChanderPenTest using Python By Purna Chander
PenTest using Python By Purna Chander
 
File io
File io File io
File io
 
Golang design4concurrency
Golang design4concurrencyGolang design4concurrency
Golang design4concurrency
 
Torch intro
Torch introTorch intro
Torch intro
 
Java nio ( new io )
Java nio ( new io )Java nio ( new io )
Java nio ( new io )
 

Similar a Concurrency

Concurrency
ConcurrencyConcurrency
Concurrency
ehuard
 
Fabric-让部署变得简单
Fabric-让部署变得简单Fabric-让部署变得简单
Fabric-让部署变得简单
Eric Lo
 
Scale11x lxc talk
Scale11x lxc talkScale11x lxc talk
Scale11x lxc talk
dotCloud
 
Everything Rubinius
Everything RubiniusEverything Rubinius
Everything Rubinius
Engine Yard
 
Rubinius @ RubyAndRails2010
Rubinius @ RubyAndRails2010Rubinius @ RubyAndRails2010
Rubinius @ RubyAndRails2010
Dirkjan Bussink
 
LXC Containers and AUFs
LXC Containers and AUFsLXC Containers and AUFs
LXC Containers and AUFs
Docker, Inc.
 

Similar a Concurrency (20)

Concurrency: Rubies, Plural
Concurrency: Rubies, PluralConcurrency: Rubies, Plural
Concurrency: Rubies, Plural
 
Concurrency: Rubies, plural
Concurrency: Rubies, pluralConcurrency: Rubies, plural
Concurrency: Rubies, plural
 
Concurrency
ConcurrencyConcurrency
Concurrency
 
Fabric-让部署变得简单
Fabric-让部署变得简单Fabric-让部署变得简单
Fabric-让部署变得简单
 
Acceptance & Integration Testing With Behat (PBC11)
Acceptance & Integration Testing With Behat (PBC11)Acceptance & Integration Testing With Behat (PBC11)
Acceptance & Integration Testing With Behat (PBC11)
 
ActiveRecord 2.3
ActiveRecord 2.3ActiveRecord 2.3
ActiveRecord 2.3
 
Lightweight Virtualization: LXC containers & AUFS
Lightweight Virtualization: LXC containers & AUFSLightweight Virtualization: LXC containers & AUFS
Lightweight Virtualization: LXC containers & AUFS
 
Rejectkaigi 2010
Rejectkaigi 2010Rejectkaigi 2010
Rejectkaigi 2010
 
Scale11x lxc talk
Scale11x lxc talkScale11x lxc talk
Scale11x lxc talk
 
Plone on RelStorage
Plone on RelStoragePlone on RelStorage
Plone on RelStorage
 
Everything Rubinius
Everything RubiniusEverything Rubinius
Everything Rubinius
 
Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017
Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017
Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017
 
A Quick Introduction to Linux
A Quick Introduction to LinuxA Quick Introduction to Linux
A Quick Introduction to Linux
 
Rubinius @ RubyAndRails2010
Rubinius @ RubyAndRails2010Rubinius @ RubyAndRails2010
Rubinius @ RubyAndRails2010
 
Intro to Multitasking
Intro to MultitaskingIntro to Multitasking
Intro to Multitasking
 
LXC Containers and AUFs
LXC Containers and AUFsLXC Containers and AUFs
LXC Containers and AUFs
 
Chapter 6 os
Chapter 6 osChapter 6 os
Chapter 6 os
 
Unix_basics
Unix_basicsUnix_basics
Unix_basics
 
Fluentd introduction at ipros
Fluentd introduction at iprosFluentd introduction at ipros
Fluentd introduction at ipros
 
Programación funcional con haskell
Programación funcional con haskellProgramación funcional con haskell
Programación funcional con haskell
 

Más de ehuard

Ruby goes to hollywood
Ruby goes to hollywoodRuby goes to hollywood
Ruby goes to hollywood
ehuard
 
Ruby hollywood
Ruby hollywoodRuby hollywood
Ruby hollywood
ehuard
 

Más de ehuard (13)

Euroclojure 2017
Euroclojure 2017Euroclojure 2017
Euroclojure 2017
 
Ruby goes to Hollywood
Ruby goes to HollywoodRuby goes to Hollywood
Ruby goes to Hollywood
 
Ruby hollywood nordic
Ruby hollywood nordicRuby hollywood nordic
Ruby hollywood nordic
 
Ruby goes to hollywood
Ruby goes to hollywoodRuby goes to hollywood
Ruby goes to hollywood
 
Ruby hollywood
Ruby hollywoodRuby hollywood
Ruby hollywood
 
12 hours to rate a rails application
12 hours to rate a rails application12 hours to rate a rails application
12 hours to rate a rails application
 
how to rate a Rails application
how to rate a Rails applicationhow to rate a Rails application
how to rate a Rails application
 
12 Hours To Rate A Rails Application
12 Hours To Rate A Rails Application12 Hours To Rate A Rails Application
12 Hours To Rate A Rails Application
 
Barcamp Ghent2009
Barcamp Ghent2009Barcamp Ghent2009
Barcamp Ghent2009
 
Tokyo Cabinet
Tokyo CabinetTokyo Cabinet
Tokyo Cabinet
 
The real-time web
The real-time webThe real-time web
The real-time web
 
Rails and the internet of things
Rails and the internet of thingsRails and the internet of things
Rails and the internet of things
 
Oauth
OauthOauth
Oauth
 

Último

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Último (20)

08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 

Concurrency