3. Detour #1: subroutines
vs. coroutines
• sub-routines
• every function you’ve ever written
• single entry-point & single exit
• co-routines
• single entry-point, multiple exit & re-
entry points
4. Detour #1.a:
subroutines
def a_sub_routine( )
CPU
end
24. Detour.do {|d| talk << 4}
# Non Evented
open('http://lrug.org/').read #=> ‘<html....
# Evented
class HTTPClient
def receive_body(data)
@data << data
end
end
http_client = HTTPClient.new
EventMachine::run do
EventMachine::connect 'lrug.org', 80, http_client
end
http_client.data #=> ‘<html....
25. So…what is a practical
use for a fiber?
http://www.espace.com.eg/neverblock/
26. What I didn’t say
• The rest of the API
• fiber_instance.transfer - invoke on a Fiber to pass
control to it, instead of yielding to the caller
• fiber_instance.alive? - can we safely resume this
Fiber, or has it terminated?
• Fiber.current - get the current Fiber so we can play
with it
• Lightweight - less memory over head than threads
• The downsides - single core only really
28. Resources
• http://delicious.com/hlame/fibers
• (most of the stuff I researched is here)
• http://github.com/oldmoe/neverblock
• http://en.wikipedia.org/wiki/Fiber_(computer_science)
• http://en.wikipedia.org/wiki/Coroutine
Hinweis der Redaktion
I'm going to talk about Fibers in Ruby 1.9.
Keyword is rough
no knowledge
not ruby 1.9 dayjob
nor in spare time (lazy)
researched in last week
apologies for any ommissions (there will be some)
if you know it, don&#x2019;t ask mean questions
sorry
--
The key word in my title is "rough"; I'm coming to the material with little or no practical knowledge. I'm not using 1.9 in my day job, and although I probably would use it for any spare-time hacking, it's very rare that I get down to any as I'm, basically, lazy.
So apologies to anyone that knows this stuff already; I might get things wrong, or not cover everything in enough detail. I'm sorry.
2 ideas that should sound familiar:
co-routines & co-operative multitasking
co-routines = familiar bcz sub-routines
co-operative multitasking = pre-emptive multitasking
detour to cover each of these ideas then onto ruby
--
Fibers are an implementation of 2 important ideas:
1. The first idea is &#x201C;co-routines&#x201D; (and this should sound familiar, as you&#x2019;ll have heard of sub-routines which are related)
and
2. The second idea is &#x201C;co-operative multitasking&#x201D; (and again, you should recognise this as similar sounding to &#x201C;pre-emptive mutlitasking&#x201D;).
So, we'll take a quick detour to cover these in turn and then we'll come back to Ruby.
sub-routine invoke =
start on first line, proceed to end, STOP
go-in, come out
co-routines are different
start on first line, proceed to end, STOP
in between = take detour come back later
been around a bit
but hardly implemented
--
So pretty much every method or function you&#x2019;ve ever written is a sub-routine. When you invoke them you start at the first line and run through them till they terminate and give you their result.
A co-routine is a little bit different. When you invoke them they also start on the first line of code but they can halt execution and exit before they terminate. Later you can then re-enter and resume execution from where you left off.
It&#x2019;s also unlikely you&#x2019;ll have written one, yet, as despite being around for a while not many languages provide them as a feature.
-----
Every method or function you write is a sub-routine. It's a package of code that has an entry point, and a single exit point. Admittedly things like exceptions and multiple return paths might confuse this and make it seem like you have many exit points, but for each *single run through the code* there's one path: you go in, do something and you come out and that's it.
To clarify:
invoke subroutine -style method.
CPU enters method
bounce around
until execution stops
with return
(or exception)
(or implicit last statement)
and release CPU to caller
---
So, here&#x2019;s a simple subroutine example.
When you call a method the flow of control enters the function, and is trapped until the method terminates.
Once the method terminates, here with an explicit return, but it could be an exception, or simply stopping after the last executable statement of the code path, the flow of control is finally released to the caller.
The only way to go back into the function is to go back to the start by calling it again.
To clarify:
invoke subroutine -style method.
CPU enters method
bounce around
until execution stops
with return
(or exception)
(or implicit last statement)
and release CPU to caller
---
So, here&#x2019;s a simple subroutine example.
When you call a method the flow of control enters the function, and is trapped until the method terminates.
Once the method terminates, here with an explicit return, but it could be an exception, or simply stopping after the last executable statement of the code path, the flow of control is finally released to the caller.
The only way to go back into the function is to go back to the start by calling it again.
To clarify:
invoke subroutine -style method.
CPU enters method
bounce around
until execution stops
with return
(or exception)
(or implicit last statement)
and release CPU to caller
---
So, here&#x2019;s a simple subroutine example.
When you call a method the flow of control enters the function, and is trapped until the method terminates.
Once the method terminates, here with an explicit return, but it could be an exception, or simply stopping after the last executable statement of the code path, the flow of control is finally released to the caller.
The only way to go back into the function is to go back to the start by calling it again.
To clarify:
invoke subroutine -style method.
CPU enters method
bounce around
until execution stops
with return
(or exception)
(or implicit last statement)
and release CPU to caller
---
So, here&#x2019;s a simple subroutine example.
When you call a method the flow of control enters the function, and is trapped until the method terminates.
Once the method terminates, here with an explicit return, but it could be an exception, or simply stopping after the last executable statement of the code path, the flow of control is finally released to the caller.
The only way to go back into the function is to go back to the start by calling it again.
To clarify:
invoke subroutine -style method.
CPU enters method
bounce around
until execution stops
with return
(or exception)
(or implicit last statement)
and release CPU to caller
---
So, here&#x2019;s a simple subroutine example.
When you call a method the flow of control enters the function, and is trapped until the method terminates.
Once the method terminates, here with an explicit return, but it could be an exception, or simply stopping after the last executable statement of the code path, the flow of control is finally released to the caller.
The only way to go back into the function is to go back to the start by calling it again.
To clarify:
invoke subroutine -style method.
CPU enters method
bounce around
until execution stops
with return
(or exception)
(or implicit last statement)
and release CPU to caller
---
So, here&#x2019;s a simple subroutine example.
When you call a method the flow of control enters the function, and is trapped until the method terminates.
Once the method terminates, here with an explicit return, but it could be an exception, or simply stopping after the last executable statement of the code path, the flow of control is finally released to the caller.
The only way to go back into the function is to go back to the start by calling it again.
To clarify:
invoke subroutine -style method.
CPU enters method
bounce around
until execution stops
with return
(or exception)
(or implicit last statement)
and release CPU to caller
---
So, here&#x2019;s a simple subroutine example.
When you call a method the flow of control enters the function, and is trapped until the method terminates.
Once the method terminates, here with an explicit return, but it could be an exception, or simply stopping after the last executable statement of the code path, the flow of control is finally released to the caller.
The only way to go back into the function is to go back to the start by calling it again.
To clarify:
invoke subroutine -style method.
CPU enters method
bounce around
until execution stops
with return
(or exception)
(or implicit last statement)
and release CPU to caller
---
So, here&#x2019;s a simple subroutine example.
When you call a method the flow of control enters the function, and is trapped until the method terminates.
Once the method terminates, here with an explicit return, but it could be an exception, or simply stopping after the last executable statement of the code path, the flow of control is finally released to the caller.
The only way to go back into the function is to go back to the start by calling it again.
To clarify:
invoke subroutine -style method.
CPU enters method
bounce around
until execution stops
with return
(or exception)
(or implicit last statement)
and release CPU to caller
---
So, here&#x2019;s a simple subroutine example.
When you call a method the flow of control enters the function, and is trapped until the method terminates.
Once the method terminates, here with an explicit return, but it could be an exception, or simply stopping after the last executable statement of the code path, the flow of control is finally released to the caller.
The only way to go back into the function is to go back to the start by calling it again.
To clarify:
invoke subroutine -style method.
CPU enters method
bounce around
until execution stops
with return
(or exception)
(or implicit last statement)
and release CPU to caller
---
So, here&#x2019;s a simple subroutine example.
When you call a method the flow of control enters the function, and is trapped until the method terminates.
Once the method terminates, here with an explicit return, but it could be an exception, or simply stopping after the last executable statement of the code path, the flow of control is finally released to the caller.
The only way to go back into the function is to go back to the start by calling it again.
To clarify:
invoke subroutine -style method.
CPU enters method
bounce around
until execution stops
with return
(or exception)
(or implicit last statement)
and release CPU to caller
---
So, here&#x2019;s a simple subroutine example.
When you call a method the flow of control enters the function, and is trapped until the method terminates.
Once the method terminates, here with an explicit return, but it could be an exception, or simply stopping after the last executable statement of the code path, the flow of control is finally released to the caller.
The only way to go back into the function is to go back to the start by calling it again.
To clarify:
invoke subroutine -style method.
CPU enters method
bounce around
until execution stops
with return
(or exception)
(or implicit last statement)
and release CPU to caller
---
So, here&#x2019;s a simple subroutine example.
When you call a method the flow of control enters the function, and is trapped until the method terminates.
Once the method terminates, here with an explicit return, but it could be an exception, or simply stopping after the last executable statement of the code path, the flow of control is finally released to the caller.
The only way to go back into the function is to go back to the start by calling it again.
once exited,
no going back inside
that method is dead
want to re-run?
have to re-invoke
create new copy of stack (expensive)
and enter at start
nothing shared (&#x2018;cept pass-ins)
---
So, once you exit a sub-routine, the door is closed; you can&#x2019;t return to it the way you came out.
To re-use the sub-routine, your only option is to re-invoke it and go back to the first line of code. This creates a new copy of the entire stack, so there&#x2019;s nothing shared between this invocation and the previous ones, or any future ones. Depending on your code, this could be expensive.
once exited,
no going back inside
that method is dead
want to re-run?
have to re-invoke
create new copy of stack (expensive)
and enter at start
nothing shared (&#x2018;cept pass-ins)
---
So, once you exit a sub-routine, the door is closed; you can&#x2019;t return to it the way you came out.
To re-use the sub-routine, your only option is to re-invoke it and go back to the first line of code. This creates a new copy of the entire stack, so there&#x2019;s nothing shared between this invocation and the previous ones, or any future ones. Depending on your code, this could be expensive.
once exited,
no going back inside
that method is dead
want to re-run?
have to re-invoke
create new copy of stack (expensive)
and enter at start
nothing shared (&#x2018;cept pass-ins)
---
So, once you exit a sub-routine, the door is closed; you can&#x2019;t return to it the way you came out.
To re-use the sub-routine, your only option is to re-invoke it and go back to the first line of code. This creates a new copy of the entire stack, so there&#x2019;s nothing shared between this invocation and the previous ones, or any future ones. Depending on your code, this could be expensive.
once exited,
no going back inside
that method is dead
want to re-run?
have to re-invoke
create new copy of stack (expensive)
and enter at start
nothing shared (&#x2018;cept pass-ins)
---
So, once you exit a sub-routine, the door is closed; you can&#x2019;t return to it the way you came out.
To re-use the sub-routine, your only option is to re-invoke it and go back to the first line of code. This creates a new copy of the entire stack, so there&#x2019;s nothing shared between this invocation and the previous ones, or any future ones. Depending on your code, this could be expensive.
once exited,
no going back inside
that method is dead
want to re-run?
have to re-invoke
create new copy of stack (expensive)
and enter at start
nothing shared (&#x2018;cept pass-ins)
---
So, once you exit a sub-routine, the door is closed; you can&#x2019;t return to it the way you came out.
To re-use the sub-routine, your only option is to re-invoke it and go back to the first line of code. This creates a new copy of the entire stack, so there&#x2019;s nothing shared between this invocation and the previous ones, or any future ones. Depending on your code, this could be expensive.
once exited,
no going back inside
that method is dead
want to re-run?
have to re-invoke
create new copy of stack (expensive)
and enter at start
nothing shared (&#x2018;cept pass-ins)
---
So, once you exit a sub-routine, the door is closed; you can&#x2019;t return to it the way you came out.
To re-use the sub-routine, your only option is to re-invoke it and go back to the first line of code. This creates a new copy of the entire stack, so there&#x2019;s nothing shared between this invocation and the previous ones, or any future ones. Depending on your code, this could be expensive.
once exited,
no going back inside
that method is dead
want to re-run?
have to re-invoke
create new copy of stack (expensive)
and enter at start
nothing shared (&#x2018;cept pass-ins)
---
So, once you exit a sub-routine, the door is closed; you can&#x2019;t return to it the way you came out.
To re-use the sub-routine, your only option is to re-invoke it and go back to the first line of code. This creates a new copy of the entire stack, so there&#x2019;s nothing shared between this invocation and the previous ones, or any future ones. Depending on your code, this could be expensive.
once exited,
no going back inside
that method is dead
want to re-run?
have to re-invoke
create new copy of stack (expensive)
and enter at start
nothing shared (&#x2018;cept pass-ins)
---
So, once you exit a sub-routine, the door is closed; you can&#x2019;t return to it the way you came out.
To re-use the sub-routine, your only option is to re-invoke it and go back to the first line of code. This creates a new copy of the entire stack, so there&#x2019;s nothing shared between this invocation and the previous ones, or any future ones. Depending on your code, this could be expensive.
once exited,
no going back inside
that method is dead
want to re-run?
have to re-invoke
create new copy of stack (expensive)
and enter at start
nothing shared (&#x2018;cept pass-ins)
---
So, once you exit a sub-routine, the door is closed; you can&#x2019;t return to it the way you came out.
To re-use the sub-routine, your only option is to re-invoke it and go back to the first line of code. This creates a new copy of the entire stack, so there&#x2019;s nothing shared between this invocation and the previous ones, or any future ones. Depending on your code, this could be expensive.
once exited,
no going back inside
that method is dead
want to re-run?
have to re-invoke
create new copy of stack (expensive)
and enter at start
nothing shared (&#x2018;cept pass-ins)
---
So, once you exit a sub-routine, the door is closed; you can&#x2019;t return to it the way you came out.
To re-use the sub-routine, your only option is to re-invoke it and go back to the first line of code. This creates a new copy of the entire stack, so there&#x2019;s nothing shared between this invocation and the previous ones, or any future ones. Depending on your code, this could be expensive.
once exited,
no going back inside
that method is dead
want to re-run?
have to re-invoke
create new copy of stack (expensive)
and enter at start
nothing shared (&#x2018;cept pass-ins)
---
So, once you exit a sub-routine, the door is closed; you can&#x2019;t return to it the way you came out.
To re-use the sub-routine, your only option is to re-invoke it and go back to the first line of code. This creates a new copy of the entire stack, so there&#x2019;s nothing shared between this invocation and the previous ones, or any future ones. Depending on your code, this could be expensive.
once exited,
no going back inside
that method is dead
want to re-run?
have to re-invoke
create new copy of stack (expensive)
and enter at start
nothing shared (&#x2018;cept pass-ins)
---
So, once you exit a sub-routine, the door is closed; you can&#x2019;t return to it the way you came out.
To re-use the sub-routine, your only option is to re-invoke it and go back to the first line of code. This creates a new copy of the entire stack, so there&#x2019;s nothing shared between this invocation and the previous ones, or any future ones. Depending on your code, this could be expensive.
once exited,
no going back inside
that method is dead
want to re-run?
have to re-invoke
create new copy of stack (expensive)
and enter at start
nothing shared (&#x2018;cept pass-ins)
---
So, once you exit a sub-routine, the door is closed; you can&#x2019;t return to it the way you came out.
To re-use the sub-routine, your only option is to re-invoke it and go back to the first line of code. This creates a new copy of the entire stack, so there&#x2019;s nothing shared between this invocation and the previous ones, or any future ones. Depending on your code, this could be expensive.
once exited,
no going back inside
that method is dead
want to re-run?
have to re-invoke
create new copy of stack (expensive)
and enter at start
nothing shared (&#x2018;cept pass-ins)
---
So, once you exit a sub-routine, the door is closed; you can&#x2019;t return to it the way you came out.
To re-use the sub-routine, your only option is to re-invoke it and go back to the first line of code. This creates a new copy of the entire stack, so there&#x2019;s nothing shared between this invocation and the previous ones, or any future ones. Depending on your code, this could be expensive.
once exited,
no going back inside
that method is dead
want to re-run?
have to re-invoke
create new copy of stack (expensive)
and enter at start
nothing shared (&#x2018;cept pass-ins)
---
So, once you exit a sub-routine, the door is closed; you can&#x2019;t return to it the way you came out.
To re-use the sub-routine, your only option is to re-invoke it and go back to the first line of code. This creates a new copy of the entire stack, so there&#x2019;s nothing shared between this invocation and the previous ones, or any future ones. Depending on your code, this could be expensive.
once exited,
no going back inside
that method is dead
want to re-run?
have to re-invoke
create new copy of stack (expensive)
and enter at start
nothing shared (&#x2018;cept pass-ins)
---
So, once you exit a sub-routine, the door is closed; you can&#x2019;t return to it the way you came out.
To re-use the sub-routine, your only option is to re-invoke it and go back to the first line of code. This creates a new copy of the entire stack, so there&#x2019;s nothing shared between this invocation and the previous ones, or any future ones. Depending on your code, this could be expensive.
once exited,
no going back inside
that method is dead
want to re-run?
have to re-invoke
create new copy of stack (expensive)
and enter at start
nothing shared (&#x2018;cept pass-ins)
---
So, once you exit a sub-routine, the door is closed; you can&#x2019;t return to it the way you came out.
To re-use the sub-routine, your only option is to re-invoke it and go back to the first line of code. This creates a new copy of the entire stack, so there&#x2019;s nothing shared between this invocation and the previous ones, or any future ones. Depending on your code, this could be expensive.
once exited,
no going back inside
that method is dead
want to re-run?
have to re-invoke
create new copy of stack (expensive)
and enter at start
nothing shared (&#x2018;cept pass-ins)
---
So, once you exit a sub-routine, the door is closed; you can&#x2019;t return to it the way you came out.
To re-use the sub-routine, your only option is to re-invoke it and go back to the first line of code. This creates a new copy of the entire stack, so there&#x2019;s nothing shared between this invocation and the previous ones, or any future ones. Depending on your code, this could be expensive.
once exited,
no going back inside
that method is dead
want to re-run?
have to re-invoke
create new copy of stack (expensive)
and enter at start
nothing shared (&#x2018;cept pass-ins)
---
So, once you exit a sub-routine, the door is closed; you can&#x2019;t return to it the way you came out.
To re-use the sub-routine, your only option is to re-invoke it and go back to the first line of code. This creates a new copy of the entire stack, so there&#x2019;s nothing shared between this invocation and the previous ones, or any future ones. Depending on your code, this could be expensive.
once exited,
no going back inside
that method is dead
want to re-run?
have to re-invoke
create new copy of stack (expensive)
and enter at start
nothing shared (&#x2018;cept pass-ins)
---
So, once you exit a sub-routine, the door is closed; you can&#x2019;t return to it the way you came out.
To re-use the sub-routine, your only option is to re-invoke it and go back to the first line of code. This creates a new copy of the entire stack, so there&#x2019;s nothing shared between this invocation and the previous ones, or any future ones. Depending on your code, this could be expensive.
how are co-routines different?
start&#x2019;s same
invoke method
CPU trapped
execute statements
exit with yield
gives caller back CPU
caller later resume
re-enter co-routine
at EXACT POINT WHERE WE LEFT OFF
same stack, same everything
continue exec
---
And here&#x2019;s a similar example for a co-routine.
It starts pretty much the same way. The flow of control enters the method and is trapped until it provides a result, this time with a yield. However, unlike before, we can resume the method and send the flow of control back in to continue working, picking up where we were when we left off.
how are co-routines different?
start&#x2019;s same
invoke method
CPU trapped
execute statements
exit with yield
gives caller back CPU
caller later resume
re-enter co-routine
at EXACT POINT WHERE WE LEFT OFF
same stack, same everything
continue exec
---
And here&#x2019;s a similar example for a co-routine.
It starts pretty much the same way. The flow of control enters the method and is trapped until it provides a result, this time with a yield. However, unlike before, we can resume the method and send the flow of control back in to continue working, picking up where we were when we left off.
how are co-routines different?
start&#x2019;s same
invoke method
CPU trapped
execute statements
exit with yield
gives caller back CPU
caller later resume
re-enter co-routine
at EXACT POINT WHERE WE LEFT OFF
same stack, same everything
continue exec
---
And here&#x2019;s a similar example for a co-routine.
It starts pretty much the same way. The flow of control enters the method and is trapped until it provides a result, this time with a yield. However, unlike before, we can resume the method and send the flow of control back in to continue working, picking up where we were when we left off.
how are co-routines different?
start&#x2019;s same
invoke method
CPU trapped
execute statements
exit with yield
gives caller back CPU
caller later resume
re-enter co-routine
at EXACT POINT WHERE WE LEFT OFF
same stack, same everything
continue exec
---
And here&#x2019;s a similar example for a co-routine.
It starts pretty much the same way. The flow of control enters the method and is trapped until it provides a result, this time with a yield. However, unlike before, we can resume the method and send the flow of control back in to continue working, picking up where we were when we left off.
how are co-routines different?
start&#x2019;s same
invoke method
CPU trapped
execute statements
exit with yield
gives caller back CPU
caller later resume
re-enter co-routine
at EXACT POINT WHERE WE LEFT OFF
same stack, same everything
continue exec
---
And here&#x2019;s a similar example for a co-routine.
It starts pretty much the same way. The flow of control enters the method and is trapped until it provides a result, this time with a yield. However, unlike before, we can resume the method and send the flow of control back in to continue working, picking up where we were when we left off.
how are co-routines different?
start&#x2019;s same
invoke method
CPU trapped
execute statements
exit with yield
gives caller back CPU
caller later resume
re-enter co-routine
at EXACT POINT WHERE WE LEFT OFF
same stack, same everything
continue exec
---
And here&#x2019;s a similar example for a co-routine.
It starts pretty much the same way. The flow of control enters the method and is trapped until it provides a result, this time with a yield. However, unlike before, we can resume the method and send the flow of control back in to continue working, picking up where we were when we left off.
how are co-routines different?
start&#x2019;s same
invoke method
CPU trapped
execute statements
exit with yield
gives caller back CPU
caller later resume
re-enter co-routine
at EXACT POINT WHERE WE LEFT OFF
same stack, same everything
continue exec
---
And here&#x2019;s a similar example for a co-routine.
It starts pretty much the same way. The flow of control enters the method and is trapped until it provides a result, this time with a yield. However, unlike before, we can resume the method and send the flow of control back in to continue working, picking up where we were when we left off.
how are co-routines different?
start&#x2019;s same
invoke method
CPU trapped
execute statements
exit with yield
gives caller back CPU
caller later resume
re-enter co-routine
at EXACT POINT WHERE WE LEFT OFF
same stack, same everything
continue exec
---
And here&#x2019;s a similar example for a co-routine.
It starts pretty much the same way. The flow of control enters the method and is trapped until it provides a result, this time with a yield. However, unlike before, we can resume the method and send the flow of control back in to continue working, picking up where we were when we left off.
how are co-routines different?
start&#x2019;s same
invoke method
CPU trapped
execute statements
exit with yield
gives caller back CPU
caller later resume
re-enter co-routine
at EXACT POINT WHERE WE LEFT OFF
same stack, same everything
continue exec
---
And here&#x2019;s a similar example for a co-routine.
It starts pretty much the same way. The flow of control enters the method and is trapped until it provides a result, this time with a yield. However, unlike before, we can resume the method and send the flow of control back in to continue working, picking up where we were when we left off.
how are co-routines different?
start&#x2019;s same
invoke method
CPU trapped
execute statements
exit with yield
gives caller back CPU
caller later resume
re-enter co-routine
at EXACT POINT WHERE WE LEFT OFF
same stack, same everything
continue exec
---
And here&#x2019;s a similar example for a co-routine.
It starts pretty much the same way. The flow of control enters the method and is trapped until it provides a result, this time with a yield. However, unlike before, we can resume the method and send the flow of control back in to continue working, picking up where we were when we left off.
how are co-routines different?
start&#x2019;s same
invoke method
CPU trapped
execute statements
exit with yield
gives caller back CPU
caller later resume
re-enter co-routine
at EXACT POINT WHERE WE LEFT OFF
same stack, same everything
continue exec
---
And here&#x2019;s a similar example for a co-routine.
It starts pretty much the same way. The flow of control enters the method and is trapped until it provides a result, this time with a yield. However, unlike before, we can resume the method and send the flow of control back in to continue working, picking up where we were when we left off.
how are co-routines different?
start&#x2019;s same
invoke method
CPU trapped
execute statements
exit with yield
gives caller back CPU
caller later resume
re-enter co-routine
at EXACT POINT WHERE WE LEFT OFF
same stack, same everything
continue exec
---
And here&#x2019;s a similar example for a co-routine.
It starts pretty much the same way. The flow of control enters the method and is trapped until it provides a result, this time with a yield. However, unlike before, we can resume the method and send the flow of control back in to continue working, picking up where we were when we left off.
how are co-routines different?
start&#x2019;s same
invoke method
CPU trapped
execute statements
exit with yield
gives caller back CPU
caller later resume
re-enter co-routine
at EXACT POINT WHERE WE LEFT OFF
same stack, same everything
continue exec
---
And here&#x2019;s a similar example for a co-routine.
It starts pretty much the same way. The flow of control enters the method and is trapped until it provides a result, this time with a yield. However, unlike before, we can resume the method and send the flow of control back in to continue working, picking up where we were when we left off.
how are co-routines different?
start&#x2019;s same
invoke method
CPU trapped
execute statements
exit with yield
gives caller back CPU
caller later resume
re-enter co-routine
at EXACT POINT WHERE WE LEFT OFF
same stack, same everything
continue exec
---
And here&#x2019;s a similar example for a co-routine.
It starts pretty much the same way. The flow of control enters the method and is trapped until it provides a result, this time with a yield. However, unlike before, we can resume the method and send the flow of control back in to continue working, picking up where we were when we left off.
how are co-routines different?
start&#x2019;s same
invoke method
CPU trapped
execute statements
exit with yield
gives caller back CPU
caller later resume
re-enter co-routine
at EXACT POINT WHERE WE LEFT OFF
same stack, same everything
continue exec
---
And here&#x2019;s a similar example for a co-routine.
It starts pretty much the same way. The flow of control enters the method and is trapped until it provides a result, this time with a yield. However, unlike before, we can resume the method and send the flow of control back in to continue working, picking up where we were when we left off.
how are co-routines different?
start&#x2019;s same
invoke method
CPU trapped
execute statements
exit with yield
gives caller back CPU
caller later resume
re-enter co-routine
at EXACT POINT WHERE WE LEFT OFF
same stack, same everything
continue exec
---
And here&#x2019;s a similar example for a co-routine.
It starts pretty much the same way. The flow of control enters the method and is trapped until it provides a result, this time with a yield. However, unlike before, we can resume the method and send the flow of control back in to continue working, picking up where we were when we left off.
more interesting is...
not a one-time deal
yield to the caller
caller resume routine
many times!
even more interesting
yield from multiple places
and resume knows which yield to go back to
---
What makes co-routines even more interesting is that we can yield and resume as many times as we want, until, of course, the co-routine comes to a natural termination.
We can also have as many yield&#x2019;s as we want, we don&#x2019;t always have to yield from the same place. Although having yielded at a given point, we resume at that point, we can&#x2019;t choose some other yield point to re-enter at.
more interesting is...
not a one-time deal
yield to the caller
caller resume routine
many times!
even more interesting
yield from multiple places
and resume knows which yield to go back to
---
What makes co-routines even more interesting is that we can yield and resume as many times as we want, until, of course, the co-routine comes to a natural termination.
We can also have as many yield&#x2019;s as we want, we don&#x2019;t always have to yield from the same place. Although having yielded at a given point, we resume at that point, we can&#x2019;t choose some other yield point to re-enter at.
more interesting is...
not a one-time deal
yield to the caller
caller resume routine
many times!
even more interesting
yield from multiple places
and resume knows which yield to go back to
---
What makes co-routines even more interesting is that we can yield and resume as many times as we want, until, of course, the co-routine comes to a natural termination.
We can also have as many yield&#x2019;s as we want, we don&#x2019;t always have to yield from the same place. Although having yielded at a given point, we resume at that point, we can&#x2019;t choose some other yield point to re-enter at.
more interesting is...
not a one-time deal
yield to the caller
caller resume routine
many times!
even more interesting
yield from multiple places
and resume knows which yield to go back to
---
What makes co-routines even more interesting is that we can yield and resume as many times as we want, until, of course, the co-routine comes to a natural termination.
We can also have as many yield&#x2019;s as we want, we don&#x2019;t always have to yield from the same place. Although having yielded at a given point, we resume at that point, we can&#x2019;t choose some other yield point to re-enter at.
more interesting is...
not a one-time deal
yield to the caller
caller resume routine
many times!
even more interesting
yield from multiple places
and resume knows which yield to go back to
---
What makes co-routines even more interesting is that we can yield and resume as many times as we want, until, of course, the co-routine comes to a natural termination.
We can also have as many yield&#x2019;s as we want, we don&#x2019;t always have to yield from the same place. Although having yielded at a given point, we resume at that point, we can&#x2019;t choose some other yield point to re-enter at.
more interesting is...
not a one-time deal
yield to the caller
caller resume routine
many times!
even more interesting
yield from multiple places
and resume knows which yield to go back to
---
What makes co-routines even more interesting is that we can yield and resume as many times as we want, until, of course, the co-routine comes to a natural termination.
We can also have as many yield&#x2019;s as we want, we don&#x2019;t always have to yield from the same place. Although having yielded at a given point, we resume at that point, we can&#x2019;t choose some other yield point to re-enter at.
more interesting is...
not a one-time deal
yield to the caller
caller resume routine
many times!
even more interesting
yield from multiple places
and resume knows which yield to go back to
---
What makes co-routines even more interesting is that we can yield and resume as many times as we want, until, of course, the co-routine comes to a natural termination.
We can also have as many yield&#x2019;s as we want, we don&#x2019;t always have to yield from the same place. Although having yielded at a given point, we resume at that point, we can&#x2019;t choose some other yield point to re-enter at.
more interesting is...
not a one-time deal
yield to the caller
caller resume routine
many times!
even more interesting
yield from multiple places
and resume knows which yield to go back to
---
What makes co-routines even more interesting is that we can yield and resume as many times as we want, until, of course, the co-routine comes to a natural termination.
We can also have as many yield&#x2019;s as we want, we don&#x2019;t always have to yield from the same place. Although having yielded at a given point, we resume at that point, we can&#x2019;t choose some other yield point to re-enter at.
more interesting is...
not a one-time deal
yield to the caller
caller resume routine
many times!
even more interesting
yield from multiple places
and resume knows which yield to go back to
---
What makes co-routines even more interesting is that we can yield and resume as many times as we want, until, of course, the co-routine comes to a natural termination.
We can also have as many yield&#x2019;s as we want, we don&#x2019;t always have to yield from the same place. Although having yielded at a given point, we resume at that point, we can&#x2019;t choose some other yield point to re-enter at.
more interesting is...
not a one-time deal
yield to the caller
caller resume routine
many times!
even more interesting
yield from multiple places
and resume knows which yield to go back to
---
What makes co-routines even more interesting is that we can yield and resume as many times as we want, until, of course, the co-routine comes to a natural termination.
We can also have as many yield&#x2019;s as we want, we don&#x2019;t always have to yield from the same place. Although having yielded at a given point, we resume at that point, we can&#x2019;t choose some other yield point to re-enter at.
more interesting is...
not a one-time deal
yield to the caller
caller resume routine
many times!
even more interesting
yield from multiple places
and resume knows which yield to go back to
---
What makes co-routines even more interesting is that we can yield and resume as many times as we want, until, of course, the co-routine comes to a natural termination.
We can also have as many yield&#x2019;s as we want, we don&#x2019;t always have to yield from the same place. Although having yielded at a given point, we resume at that point, we can&#x2019;t choose some other yield point to re-enter at.
more interesting is...
not a one-time deal
yield to the caller
caller resume routine
many times!
even more interesting
yield from multiple places
and resume knows which yield to go back to
---
What makes co-routines even more interesting is that we can yield and resume as many times as we want, until, of course, the co-routine comes to a natural termination.
We can also have as many yield&#x2019;s as we want, we don&#x2019;t always have to yield from the same place. Although having yielded at a given point, we resume at that point, we can&#x2019;t choose some other yield point to re-enter at.
more interesting is...
not a one-time deal
yield to the caller
caller resume routine
many times!
even more interesting
yield from multiple places
and resume knows which yield to go back to
---
What makes co-routines even more interesting is that we can yield and resume as many times as we want, until, of course, the co-routine comes to a natural termination.
We can also have as many yield&#x2019;s as we want, we don&#x2019;t always have to yield from the same place. Although having yielded at a given point, we resume at that point, we can&#x2019;t choose some other yield point to re-enter at.
more interesting is...
not a one-time deal
yield to the caller
caller resume routine
many times!
even more interesting
yield from multiple places
and resume knows which yield to go back to
---
What makes co-routines even more interesting is that we can yield and resume as many times as we want, until, of course, the co-routine comes to a natural termination.
We can also have as many yield&#x2019;s as we want, we don&#x2019;t always have to yield from the same place. Although having yielded at a given point, we resume at that point, we can&#x2019;t choose some other yield point to re-enter at.
more interesting is...
not a one-time deal
yield to the caller
caller resume routine
many times!
even more interesting
yield from multiple places
and resume knows which yield to go back to
---
What makes co-routines even more interesting is that we can yield and resume as many times as we want, until, of course, the co-routine comes to a natural termination.
We can also have as many yield&#x2019;s as we want, we don&#x2019;t always have to yield from the same place. Although having yielded at a given point, we resume at that point, we can&#x2019;t choose some other yield point to re-enter at.
more interesting is...
not a one-time deal
yield to the caller
caller resume routine
many times!
even more interesting
yield from multiple places
and resume knows which yield to go back to
---
What makes co-routines even more interesting is that we can yield and resume as many times as we want, until, of course, the co-routine comes to a natural termination.
We can also have as many yield&#x2019;s as we want, we don&#x2019;t always have to yield from the same place. Although having yielded at a given point, we resume at that point, we can&#x2019;t choose some other yield point to re-enter at.
more interesting is...
not a one-time deal
yield to the caller
caller resume routine
many times!
even more interesting
yield from multiple places
and resume knows which yield to go back to
---
What makes co-routines even more interesting is that we can yield and resume as many times as we want, until, of course, the co-routine comes to a natural termination.
We can also have as many yield&#x2019;s as we want, we don&#x2019;t always have to yield from the same place. Although having yielded at a given point, we resume at that point, we can&#x2019;t choose some other yield point to re-enter at.
more interesting is...
not a one-time deal
yield to the caller
caller resume routine
many times!
even more interesting
yield from multiple places
and resume knows which yield to go back to
---
What makes co-routines even more interesting is that we can yield and resume as many times as we want, until, of course, the co-routine comes to a natural termination.
We can also have as many yield&#x2019;s as we want, we don&#x2019;t always have to yield from the same place. Although having yielded at a given point, we resume at that point, we can&#x2019;t choose some other yield point to re-enter at.
more interesting is...
not a one-time deal
yield to the caller
caller resume routine
many times!
even more interesting
yield from multiple places
and resume knows which yield to go back to
---
What makes co-routines even more interesting is that we can yield and resume as many times as we want, until, of course, the co-routine comes to a natural termination.
We can also have as many yield&#x2019;s as we want, we don&#x2019;t always have to yield from the same place. Although having yielded at a given point, we resume at that point, we can&#x2019;t choose some other yield point to re-enter at.
more interesting is...
not a one-time deal
yield to the caller
caller resume routine
many times!
even more interesting
yield from multiple places
and resume knows which yield to go back to
---
What makes co-routines even more interesting is that we can yield and resume as many times as we want, until, of course, the co-routine comes to a natural termination.
We can also have as many yield&#x2019;s as we want, we don&#x2019;t always have to yield from the same place. Although having yielded at a given point, we resume at that point, we can&#x2019;t choose some other yield point to re-enter at.
more interesting is...
not a one-time deal
yield to the caller
caller resume routine
many times!
even more interesting
yield from multiple places
and resume knows which yield to go back to
---
What makes co-routines even more interesting is that we can yield and resume as many times as we want, until, of course, the co-routine comes to a natural termination.
We can also have as many yield&#x2019;s as we want, we don&#x2019;t always have to yield from the same place. Although having yielded at a given point, we resume at that point, we can&#x2019;t choose some other yield point to re-enter at.
2nd idea - multitasking
1st = thread model
several running tasks
OS or lang runtime schedules
don&#x2019;t know when so access shared objects = pain (locks)
Fibers = 2nd
programmer has control
choose when in each task to give up CPU
and who to give it to
--
You should be familiar with pre-emptive multitasking as it&#x2019;s the standard model of concurrency used by most Thread implementations.
You have several tasks running at the same time, scheduled by the OS or language runtime.
The gotcha is access to shared objects.
Fiber&#x2019;s however use the co-operative model.
With this no tasks run at the exact same time and it&#x2019;s up to the programmer to decide when each task will give up control and who to pass control onto.
2 threads, alpha, beta
scheduler gives each some CPU time
for work
they don&#x2019;t know when
so alpha wants shared data
locks it
stops changes when CPU elsewhere
when beta gets the CPU
if shared data is locked, it can&#x2019;t use it,
probably can&#x2019;t do anything, wasted effort
---
The main problem with pre-emptive multitasking is that (on a single core machine) these two threads are given CPU time arbitrarily by some scheduler. They don&#x2019;t know when in their life-cycle this&#x2019;ll happen, so when thread alpha wants to access the shared data, it has to lock it. Unfortunately this means the shared data could remain locked while thread beta has the CPU time, so thread beta can&#x2019;t do anything.
2 threads, alpha, beta
scheduler gives each some CPU time
for work
they don&#x2019;t know when
so alpha wants shared data
locks it
stops changes when CPU elsewhere
when beta gets the CPU
if shared data is locked, it can&#x2019;t use it,
probably can&#x2019;t do anything, wasted effort
---
The main problem with pre-emptive multitasking is that (on a single core machine) these two threads are given CPU time arbitrarily by some scheduler. They don&#x2019;t know when in their life-cycle this&#x2019;ll happen, so when thread alpha wants to access the shared data, it has to lock it. Unfortunately this means the shared data could remain locked while thread beta has the CPU time, so thread beta can&#x2019;t do anything.
2 threads, alpha, beta
scheduler gives each some CPU time
for work
they don&#x2019;t know when
so alpha wants shared data
locks it
stops changes when CPU elsewhere
when beta gets the CPU
if shared data is locked, it can&#x2019;t use it,
probably can&#x2019;t do anything, wasted effort
---
The main problem with pre-emptive multitasking is that (on a single core machine) these two threads are given CPU time arbitrarily by some scheduler. They don&#x2019;t know when in their life-cycle this&#x2019;ll happen, so when thread alpha wants to access the shared data, it has to lock it. Unfortunately this means the shared data could remain locked while thread beta has the CPU time, so thread beta can&#x2019;t do anything.
2 threads, alpha, beta
scheduler gives each some CPU time
for work
they don&#x2019;t know when
so alpha wants shared data
locks it
stops changes when CPU elsewhere
when beta gets the CPU
if shared data is locked, it can&#x2019;t use it,
probably can&#x2019;t do anything, wasted effort
---
The main problem with pre-emptive multitasking is that (on a single core machine) these two threads are given CPU time arbitrarily by some scheduler. They don&#x2019;t know when in their life-cycle this&#x2019;ll happen, so when thread alpha wants to access the shared data, it has to lock it. Unfortunately this means the shared data could remain locked while thread beta has the CPU time, so thread beta can&#x2019;t do anything.
2 threads, alpha, beta
scheduler gives each some CPU time
for work
they don&#x2019;t know when
so alpha wants shared data
locks it
stops changes when CPU elsewhere
when beta gets the CPU
if shared data is locked, it can&#x2019;t use it,
probably can&#x2019;t do anything, wasted effort
---
The main problem with pre-emptive multitasking is that (on a single core machine) these two threads are given CPU time arbitrarily by some scheduler. They don&#x2019;t know when in their life-cycle this&#x2019;ll happen, so when thread alpha wants to access the shared data, it has to lock it. Unfortunately this means the shared data could remain locked while thread beta has the CPU time, so thread beta can&#x2019;t do anything.
2 threads, alpha, beta
scheduler gives each some CPU time
for work
they don&#x2019;t know when
so alpha wants shared data
locks it
stops changes when CPU elsewhere
when beta gets the CPU
if shared data is locked, it can&#x2019;t use it,
probably can&#x2019;t do anything, wasted effort
---
The main problem with pre-emptive multitasking is that (on a single core machine) these two threads are given CPU time arbitrarily by some scheduler. They don&#x2019;t know when in their life-cycle this&#x2019;ll happen, so when thread alpha wants to access the shared data, it has to lock it. Unfortunately this means the shared data could remain locked while thread beta has the CPU time, so thread beta can&#x2019;t do anything.
2 threads, alpha, beta
scheduler gives each some CPU time
for work
they don&#x2019;t know when
so alpha wants shared data
locks it
stops changes when CPU elsewhere
when beta gets the CPU
if shared data is locked, it can&#x2019;t use it,
probably can&#x2019;t do anything, wasted effort
---
The main problem with pre-emptive multitasking is that (on a single core machine) these two threads are given CPU time arbitrarily by some scheduler. They don&#x2019;t know when in their life-cycle this&#x2019;ll happen, so when thread alpha wants to access the shared data, it has to lock it. Unfortunately this means the shared data could remain locked while thread beta has the CPU time, so thread beta can&#x2019;t do anything.
2 threads, alpha, beta
scheduler gives each some CPU time
for work
they don&#x2019;t know when
so alpha wants shared data
locks it
stops changes when CPU elsewhere
when beta gets the CPU
if shared data is locked, it can&#x2019;t use it,
probably can&#x2019;t do anything, wasted effort
---
The main problem with pre-emptive multitasking is that (on a single core machine) these two threads are given CPU time arbitrarily by some scheduler. They don&#x2019;t know when in their life-cycle this&#x2019;ll happen, so when thread alpha wants to access the shared data, it has to lock it. Unfortunately this means the shared data could remain locked while thread beta has the CPU time, so thread beta can&#x2019;t do anything.
2 threads, alpha, beta
scheduler gives each some CPU time
for work
they don&#x2019;t know when
so alpha wants shared data
locks it
stops changes when CPU elsewhere
when beta gets the CPU
if shared data is locked, it can&#x2019;t use it,
probably can&#x2019;t do anything, wasted effort
---
The main problem with pre-emptive multitasking is that (on a single core machine) these two threads are given CPU time arbitrarily by some scheduler. They don&#x2019;t know when in their life-cycle this&#x2019;ll happen, so when thread alpha wants to access the shared data, it has to lock it. Unfortunately this means the shared data could remain locked while thread beta has the CPU time, so thread beta can&#x2019;t do anything.
2 threads, alpha, beta
scheduler gives each some CPU time
for work
they don&#x2019;t know when
so alpha wants shared data
locks it
stops changes when CPU elsewhere
when beta gets the CPU
if shared data is locked, it can&#x2019;t use it,
probably can&#x2019;t do anything, wasted effort
---
The main problem with pre-emptive multitasking is that (on a single core machine) these two threads are given CPU time arbitrarily by some scheduler. They don&#x2019;t know when in their life-cycle this&#x2019;ll happen, so when thread alpha wants to access the shared data, it has to lock it. Unfortunately this means the shared data could remain locked while thread beta has the CPU time, so thread beta can&#x2019;t do anything.
2 threads, alpha, beta
scheduler gives each some CPU time
for work
they don&#x2019;t know when
so alpha wants shared data
locks it
stops changes when CPU elsewhere
when beta gets the CPU
if shared data is locked, it can&#x2019;t use it,
probably can&#x2019;t do anything, wasted effort
---
The main problem with pre-emptive multitasking is that (on a single core machine) these two threads are given CPU time arbitrarily by some scheduler. They don&#x2019;t know when in their life-cycle this&#x2019;ll happen, so when thread alpha wants to access the shared data, it has to lock it. Unfortunately this means the shared data could remain locked while thread beta has the CPU time, so thread beta can&#x2019;t do anything.
2 threads, alpha, beta
scheduler gives each some CPU time
for work
they don&#x2019;t know when
so alpha wants shared data
locks it
stops changes when CPU elsewhere
when beta gets the CPU
if shared data is locked, it can&#x2019;t use it,
probably can&#x2019;t do anything, wasted effort
---
The main problem with pre-emptive multitasking is that (on a single core machine) these two threads are given CPU time arbitrarily by some scheduler. They don&#x2019;t know when in their life-cycle this&#x2019;ll happen, so when thread alpha wants to access the shared data, it has to lock it. Unfortunately this means the shared data could remain locked while thread beta has the CPU time, so thread beta can&#x2019;t do anything.
2 threads, alpha, beta
scheduler gives each some CPU time
for work
they don&#x2019;t know when
so alpha wants shared data
locks it
stops changes when CPU elsewhere
when beta gets the CPU
if shared data is locked, it can&#x2019;t use it,
probably can&#x2019;t do anything, wasted effort
---
The main problem with pre-emptive multitasking is that (on a single core machine) these two threads are given CPU time arbitrarily by some scheduler. They don&#x2019;t know when in their life-cycle this&#x2019;ll happen, so when thread alpha wants to access the shared data, it has to lock it. Unfortunately this means the shared data could remain locked while thread beta has the CPU time, so thread beta can&#x2019;t do anything.
2 threads, alpha, beta
scheduler gives each some CPU time
for work
they don&#x2019;t know when
so alpha wants shared data
locks it
stops changes when CPU elsewhere
when beta gets the CPU
if shared data is locked, it can&#x2019;t use it,
probably can&#x2019;t do anything, wasted effort
---
The main problem with pre-emptive multitasking is that (on a single core machine) these two threads are given CPU time arbitrarily by some scheduler. They don&#x2019;t know when in their life-cycle this&#x2019;ll happen, so when thread alpha wants to access the shared data, it has to lock it. Unfortunately this means the shared data could remain locked while thread beta has the CPU time, so thread beta can&#x2019;t do anything.
2 threads, alpha, beta
scheduler gives each some CPU time
for work
they don&#x2019;t know when
so alpha wants shared data
locks it
stops changes when CPU elsewhere
when beta gets the CPU
if shared data is locked, it can&#x2019;t use it,
probably can&#x2019;t do anything, wasted effort
---
The main problem with pre-emptive multitasking is that (on a single core machine) these two threads are given CPU time arbitrarily by some scheduler. They don&#x2019;t know when in their life-cycle this&#x2019;ll happen, so when thread alpha wants to access the shared data, it has to lock it. Unfortunately this means the shared data could remain locked while thread beta has the CPU time, so thread beta can&#x2019;t do anything.
2 threads, alpha, beta
scheduler gives each some CPU time
for work
they don&#x2019;t know when
so alpha wants shared data
locks it
stops changes when CPU elsewhere
when beta gets the CPU
if shared data is locked, it can&#x2019;t use it,
probably can&#x2019;t do anything, wasted effort
---
The main problem with pre-emptive multitasking is that (on a single core machine) these two threads are given CPU time arbitrarily by some scheduler. They don&#x2019;t know when in their life-cycle this&#x2019;ll happen, so when thread alpha wants to access the shared data, it has to lock it. Unfortunately this means the shared data could remain locked while thread beta has the CPU time, so thread beta can&#x2019;t do anything.
2 threads, alpha, beta
scheduler gives each some CPU time
for work
they don&#x2019;t know when
so alpha wants shared data
locks it
stops changes when CPU elsewhere
when beta gets the CPU
if shared data is locked, it can&#x2019;t use it,
probably can&#x2019;t do anything, wasted effort
---
The main problem with pre-emptive multitasking is that (on a single core machine) these two threads are given CPU time arbitrarily by some scheduler. They don&#x2019;t know when in their life-cycle this&#x2019;ll happen, so when thread alpha wants to access the shared data, it has to lock it. Unfortunately this means the shared data could remain locked while thread beta has the CPU time, so thread beta can&#x2019;t do anything.
2 threads, alpha, beta
scheduler gives each some CPU time
for work
they don&#x2019;t know when
so alpha wants shared data
locks it
stops changes when CPU elsewhere
when beta gets the CPU
if shared data is locked, it can&#x2019;t use it,
probably can&#x2019;t do anything, wasted effort
---
The main problem with pre-emptive multitasking is that (on a single core machine) these two threads are given CPU time arbitrarily by some scheduler. They don&#x2019;t know when in their life-cycle this&#x2019;ll happen, so when thread alpha wants to access the shared data, it has to lock it. Unfortunately this means the shared data could remain locked while thread beta has the CPU time, so thread beta can&#x2019;t do anything.
2 threads, alpha, beta
scheduler gives each some CPU time
for work
they don&#x2019;t know when
so alpha wants shared data
locks it
stops changes when CPU elsewhere
when beta gets the CPU
if shared data is locked, it can&#x2019;t use it,
probably can&#x2019;t do anything, wasted effort
---
The main problem with pre-emptive multitasking is that (on a single core machine) these two threads are given CPU time arbitrarily by some scheduler. They don&#x2019;t know when in their life-cycle this&#x2019;ll happen, so when thread alpha wants to access the shared data, it has to lock it. Unfortunately this means the shared data could remain locked while thread beta has the CPU time, so thread beta can&#x2019;t do anything.
2 threads, alpha, beta
scheduler gives each some CPU time
for work
they don&#x2019;t know when
so alpha wants shared data
locks it
stops changes when CPU elsewhere
when beta gets the CPU
if shared data is locked, it can&#x2019;t use it,
probably can&#x2019;t do anything, wasted effort
---
The main problem with pre-emptive multitasking is that (on a single core machine) these two threads are given CPU time arbitrarily by some scheduler. They don&#x2019;t know when in their life-cycle this&#x2019;ll happen, so when thread alpha wants to access the shared data, it has to lock it. Unfortunately this means the shared data could remain locked while thread beta has the CPU time, so thread beta can&#x2019;t do anything.
2 threads, alpha, beta
scheduler gives each some CPU time
for work
they don&#x2019;t know when
so alpha wants shared data
locks it
stops changes when CPU elsewhere
when beta gets the CPU
if shared data is locked, it can&#x2019;t use it,
probably can&#x2019;t do anything, wasted effort
---
The main problem with pre-emptive multitasking is that (on a single core machine) these two threads are given CPU time arbitrarily by some scheduler. They don&#x2019;t know when in their life-cycle this&#x2019;ll happen, so when thread alpha wants to access the shared data, it has to lock it. Unfortunately this means the shared data could remain locked while thread beta has the CPU time, so thread beta can&#x2019;t do anything.
2 threads, alpha, beta
scheduler gives each some CPU time
for work
they don&#x2019;t know when
so alpha wants shared data
locks it
stops changes when CPU elsewhere
when beta gets the CPU
if shared data is locked, it can&#x2019;t use it,
probably can&#x2019;t do anything, wasted effort
---
The main problem with pre-emptive multitasking is that (on a single core machine) these two threads are given CPU time arbitrarily by some scheduler. They don&#x2019;t know when in their life-cycle this&#x2019;ll happen, so when thread alpha wants to access the shared data, it has to lock it. Unfortunately this means the shared data could remain locked while thread beta has the CPU time, so thread beta can&#x2019;t do anything.
2 threads, alpha, beta
scheduler gives each some CPU time
for work
they don&#x2019;t know when
so alpha wants shared data
locks it
stops changes when CPU elsewhere
when beta gets the CPU
if shared data is locked, it can&#x2019;t use it,
probably can&#x2019;t do anything, wasted effort
---
The main problem with pre-emptive multitasking is that (on a single core machine) these two threads are given CPU time arbitrarily by some scheduler. They don&#x2019;t know when in their life-cycle this&#x2019;ll happen, so when thread alpha wants to access the shared data, it has to lock it. Unfortunately this means the shared data could remain locked while thread beta has the CPU time, so thread beta can&#x2019;t do anything.
2 threads, alpha, beta
scheduler gives each some CPU time
for work
they don&#x2019;t know when
so alpha wants shared data
locks it
stops changes when CPU elsewhere
when beta gets the CPU
if shared data is locked, it can&#x2019;t use it,
probably can&#x2019;t do anything, wasted effort
---
The main problem with pre-emptive multitasking is that (on a single core machine) these two threads are given CPU time arbitrarily by some scheduler. They don&#x2019;t know when in their life-cycle this&#x2019;ll happen, so when thread alpha wants to access the shared data, it has to lock it. Unfortunately this means the shared data could remain locked while thread beta has the CPU time, so thread beta can&#x2019;t do anything.
2 threads, alpha, beta
scheduler gives each some CPU time
for work
they don&#x2019;t know when
so alpha wants shared data
locks it
stops changes when CPU elsewhere
when beta gets the CPU
if shared data is locked, it can&#x2019;t use it,
probably can&#x2019;t do anything, wasted effort
---
The main problem with pre-emptive multitasking is that (on a single core machine) these two threads are given CPU time arbitrarily by some scheduler. They don&#x2019;t know when in their life-cycle this&#x2019;ll happen, so when thread alpha wants to access the shared data, it has to lock it. Unfortunately this means the shared data could remain locked while thread beta has the CPU time, so thread beta can&#x2019;t do anything.
2 threads, alpha, beta
scheduler gives each some CPU time
for work
they don&#x2019;t know when
so alpha wants shared data
locks it
stops changes when CPU elsewhere
when beta gets the CPU
if shared data is locked, it can&#x2019;t use it,
probably can&#x2019;t do anything, wasted effort
---
The main problem with pre-emptive multitasking is that (on a single core machine) these two threads are given CPU time arbitrarily by some scheduler. They don&#x2019;t know when in their life-cycle this&#x2019;ll happen, so when thread alpha wants to access the shared data, it has to lock it. Unfortunately this means the shared data could remain locked while thread beta has the CPU time, so thread beta can&#x2019;t do anything.
2 threads, alpha, beta
scheduler gives each some CPU time
for work
they don&#x2019;t know when
so alpha wants shared data
locks it
stops changes when CPU elsewhere
when beta gets the CPU
if shared data is locked, it can&#x2019;t use it,
probably can&#x2019;t do anything, wasted effort
---
The main problem with pre-emptive multitasking is that (on a single core machine) these two threads are given CPU time arbitrarily by some scheduler. They don&#x2019;t know when in their life-cycle this&#x2019;ll happen, so when thread alpha wants to access the shared data, it has to lock it. Unfortunately this means the shared data could remain locked while thread beta has the CPU time, so thread beta can&#x2019;t do anything.
2 threads, alpha, beta
scheduler gives each some CPU time
for work
they don&#x2019;t know when
so alpha wants shared data
locks it
stops changes when CPU elsewhere
when beta gets the CPU
if shared data is locked, it can&#x2019;t use it,
probably can&#x2019;t do anything, wasted effort
---
The main problem with pre-emptive multitasking is that (on a single core machine) these two threads are given CPU time arbitrarily by some scheduler. They don&#x2019;t know when in their life-cycle this&#x2019;ll happen, so when thread alpha wants to access the shared data, it has to lock it. Unfortunately this means the shared data could remain locked while thread beta has the CPU time, so thread beta can&#x2019;t do anything.
2 threads, alpha, beta
scheduler gives each some CPU time
for work
they don&#x2019;t know when
so alpha wants shared data
locks it
stops changes when CPU elsewhere
when beta gets the CPU
if shared data is locked, it can&#x2019;t use it,
probably can&#x2019;t do anything, wasted effort
---
The main problem with pre-emptive multitasking is that (on a single core machine) these two threads are given CPU time arbitrarily by some scheduler. They don&#x2019;t know when in their life-cycle this&#x2019;ll happen, so when thread alpha wants to access the shared data, it has to lock it. Unfortunately this means the shared data could remain locked while thread beta has the CPU time, so thread beta can&#x2019;t do anything.
2 threads, alpha, beta
scheduler gives each some CPU time
for work
they don&#x2019;t know when
so alpha wants shared data
locks it
stops changes when CPU elsewhere
when beta gets the CPU
if shared data is locked, it can&#x2019;t use it,
probably can&#x2019;t do anything, wasted effort
---
The main problem with pre-emptive multitasking is that (on a single core machine) these two threads are given CPU time arbitrarily by some scheduler. They don&#x2019;t know when in their life-cycle this&#x2019;ll happen, so when thread alpha wants to access the shared data, it has to lock it. Unfortunately this means the shared data could remain locked while thread beta has the CPU time, so thread beta can&#x2019;t do anything.
with co-op
fibers, not threads
no external scheduler
when fiber has CPU it has CPU
can use shared data without lock
nothing else running.
when done
or done enough
transfers CPU away
other fiber picks up and starts work
---
On the other hand, in co-operative multitasking, the fiber itself has explicit control of when the CPU will transfer away. This means it doesn&#x2019;t need to lock anything because it&#x2019;s safe in the knowledge that no other fiber will be running unless it says it&#x2019;s done.
When the fiber is done (or happy that it&#x2019;s done enough for now), it stops accessing the shared data and simply transfers control away to some other fiber.
with co-op
fibers, not threads
no external scheduler
when fiber has CPU it has CPU
can use shared data without lock
nothing else running.
when done
or done enough
transfers CPU away
other fiber picks up and starts work
---
On the other hand, in co-operative multitasking, the fiber itself has explicit control of when the CPU will transfer away. This means it doesn&#x2019;t need to lock anything because it&#x2019;s safe in the knowledge that no other fiber will be running unless it says it&#x2019;s done.
When the fiber is done (or happy that it&#x2019;s done enough for now), it stops accessing the shared data and simply transfers control away to some other fiber.
with co-op
fibers, not threads
no external scheduler
when fiber has CPU it has CPU
can use shared data without lock
nothing else running.
when done
or done enough
transfers CPU away
other fiber picks up and starts work
---
On the other hand, in co-operative multitasking, the fiber itself has explicit control of when the CPU will transfer away. This means it doesn&#x2019;t need to lock anything because it&#x2019;s safe in the knowledge that no other fiber will be running unless it says it&#x2019;s done.
When the fiber is done (or happy that it&#x2019;s done enough for now), it stops accessing the shared data and simply transfers control away to some other fiber.
with co-op
fibers, not threads
no external scheduler
when fiber has CPU it has CPU
can use shared data without lock
nothing else running.
when done
or done enough
transfers CPU away
other fiber picks up and starts work
---
On the other hand, in co-operative multitasking, the fiber itself has explicit control of when the CPU will transfer away. This means it doesn&#x2019;t need to lock anything because it&#x2019;s safe in the knowledge that no other fiber will be running unless it says it&#x2019;s done.
When the fiber is done (or happy that it&#x2019;s done enough for now), it stops accessing the shared data and simply transfers control away to some other fiber.
with co-op
fibers, not threads
no external scheduler
when fiber has CPU it has CPU
can use shared data without lock
nothing else running.
when done
or done enough
transfers CPU away
other fiber picks up and starts work
---
On the other hand, in co-operative multitasking, the fiber itself has explicit control of when the CPU will transfer away. This means it doesn&#x2019;t need to lock anything because it&#x2019;s safe in the knowledge that no other fiber will be running unless it says it&#x2019;s done.
When the fiber is done (or happy that it&#x2019;s done enough for now), it stops accessing the shared data and simply transfers control away to some other fiber.
with co-op
fibers, not threads
no external scheduler
when fiber has CPU it has CPU
can use shared data without lock
nothing else running.
when done
or done enough
transfers CPU away
other fiber picks up and starts work
---
On the other hand, in co-operative multitasking, the fiber itself has explicit control of when the CPU will transfer away. This means it doesn&#x2019;t need to lock anything because it&#x2019;s safe in the knowledge that no other fiber will be running unless it says it&#x2019;s done.
When the fiber is done (or happy that it&#x2019;s done enough for now), it stops accessing the shared data and simply transfers control away to some other fiber.
with co-op
fibers, not threads
no external scheduler
when fiber has CPU it has CPU
can use shared data without lock
nothing else running.
when done
or done enough
transfers CPU away
other fiber picks up and starts work
---
On the other hand, in co-operative multitasking, the fiber itself has explicit control of when the CPU will transfer away. This means it doesn&#x2019;t need to lock anything because it&#x2019;s safe in the knowledge that no other fiber will be running unless it says it&#x2019;s done.
When the fiber is done (or happy that it&#x2019;s done enough for now), it stops accessing the shared data and simply transfers control away to some other fiber.
with co-op
fibers, not threads
no external scheduler
when fiber has CPU it has CPU
can use shared data without lock
nothing else running.
when done
or done enough
transfers CPU away
other fiber picks up and starts work
---
On the other hand, in co-operative multitasking, the fiber itself has explicit control of when the CPU will transfer away. This means it doesn&#x2019;t need to lock anything because it&#x2019;s safe in the knowledge that no other fiber will be running unless it says it&#x2019;s done.
When the fiber is done (or happy that it&#x2019;s done enough for now), it stops accessing the shared data and simply transfers control away to some other fiber.
with co-op
fibers, not threads
no external scheduler
when fiber has CPU it has CPU
can use shared data without lock
nothing else running.
when done
or done enough
transfers CPU away
other fiber picks up and starts work
---
On the other hand, in co-operative multitasking, the fiber itself has explicit control of when the CPU will transfer away. This means it doesn&#x2019;t need to lock anything because it&#x2019;s safe in the knowledge that no other fiber will be running unless it says it&#x2019;s done.
When the fiber is done (or happy that it&#x2019;s done enough for now), it stops accessing the shared data and simply transfers control away to some other fiber.
with co-op
fibers, not threads
no external scheduler
when fiber has CPU it has CPU
can use shared data without lock
nothing else running.
when done
or done enough
transfers CPU away
other fiber picks up and starts work
---
On the other hand, in co-operative multitasking, the fiber itself has explicit control of when the CPU will transfer away. This means it doesn&#x2019;t need to lock anything because it&#x2019;s safe in the knowledge that no other fiber will be running unless it says it&#x2019;s done.
When the fiber is done (or happy that it&#x2019;s done enough for now), it stops accessing the shared data and simply transfers control away to some other fiber.
with co-op
fibers, not threads
no external scheduler
when fiber has CPU it has CPU
can use shared data without lock
nothing else running.
when done
or done enough
transfers CPU away
other fiber picks up and starts work
---
On the other hand, in co-operative multitasking, the fiber itself has explicit control of when the CPU will transfer away. This means it doesn&#x2019;t need to lock anything because it&#x2019;s safe in the knowledge that no other fiber will be running unless it says it&#x2019;s done.
When the fiber is done (or happy that it&#x2019;s done enough for now), it stops accessing the shared data and simply transfers control away to some other fiber.
with co-op
fibers, not threads
no external scheduler
when fiber has CPU it has CPU
can use shared data without lock
nothing else running.
when done
or done enough
transfers CPU away
other fiber picks up and starts work
---
On the other hand, in co-operative multitasking, the fiber itself has explicit control of when the CPU will transfer away. This means it doesn&#x2019;t need to lock anything because it&#x2019;s safe in the knowledge that no other fiber will be running unless it says it&#x2019;s done.
When the fiber is done (or happy that it&#x2019;s done enough for now), it stops accessing the shared data and simply transfers control away to some other fiber.
with co-op
fibers, not threads
no external scheduler
when fiber has CPU it has CPU
can use shared data without lock
nothing else running.
when done
or done enough
transfers CPU away
other fiber picks up and starts work
---
On the other hand, in co-operative multitasking, the fiber itself has explicit control of when the CPU will transfer away. This means it doesn&#x2019;t need to lock anything because it&#x2019;s safe in the knowledge that no other fiber will be running unless it says it&#x2019;s done.
When the fiber is done (or happy that it&#x2019;s done enough for now), it stops accessing the shared data and simply transfers control away to some other fiber.
with co-op
fibers, not threads
no external scheduler
when fiber has CPU it has CPU
can use shared data without lock
nothing else running.
when done
or done enough
transfers CPU away
other fiber picks up and starts work
---
On the other hand, in co-operative multitasking, the fiber itself has explicit control of when the CPU will transfer away. This means it doesn&#x2019;t need to lock anything because it&#x2019;s safe in the knowledge that no other fiber will be running unless it says it&#x2019;s done.
When the fiber is done (or happy that it&#x2019;s done enough for now), it stops accessing the shared data and simply transfers control away to some other fiber.
science over. code now.
simple example of creating Fiber.
familiar if worked with threads
block is workload for fiber
illustrates 3 things...
Fiber.yield is exit point
shared stack (local var i same between yields)
infinite loop (!)
---
So, I&#x2019;ve bored you with the science part, how about looking at some code?
If you&#x2019;ve used threads in ruby this should be familiar. You create a Fiber by passing a block to a constructor. The block is the &#x201C;work load&#x201D; for that Fiber. In this case an infinite loop to generate increasingly excited hello&#x2019;s to the LRUG crowd. Don&#x2019;t worry about that pesky &#x201C;infinite&#x201D; though...
after create fiber
like thread, not running
call &#x201C;resume&#x201D; (chicken-before-egg)
makes fiber run from start to Fiber.yield
returns value
each successive .resume goes back in
resumes from Fiber.yield
with previous stack intact
----
So, when you create a Fiber, again just like a thread, it won&#x2019;t do anything until you ask it to. To start it you call the somewhat chicken-before-the-egg &#x201C;resume&#x201D; method. This causes hello_lrug to run until it hits that Fiber.yield. This pauses execution of the Fiber and returns the value passed to it. You also use &#x201C;resume&#x201D; to re-enter the Fiber to do some more work.
3rd interesting thing
that pesky infinite loop
it&#x2019;s ok
Fiber only runs up to .yield
then exit
CPU is out and nothing running
only call resume 5 times, never get our 6th
no longer need to think about explicit termination
lazy eval = super easy
----
So although we gave hello_lrug a workload that *will never end*, it&#x2019;s not a problem because we use the yield and resume methods to explicitly schedule when hello_lrug run. If we only want to run it 5 times and never come back to it, that&#x2019;s ok, it won&#x2019;t eat up CPU time. This gives us an interesting new way to think about writing functions; if they don&#x2019;t have to end lazy evaluation becomes super easy...
Fibonacci
standard fib method using recursion
can be hard to get head around
have to worry about termination clauses
can be expensive
(this impl will calc fib(1) several times)
---
Hey, so what&#x2019;s a talk without Fibonacci?
Here&#x2019;s the standard implementation for generating a number in the fibonacci sequence using ruby. It uses recursion, which is something you have to get your head around before you see how it works, and that can be hard sometimes, and you have to take care to have correct guard clauses to make sure you terminate the recursion.
same thing, with fibers
understanding co-routines is probably hard
both have mental roadblock
but the def is more natural
advantage, unlike recursion
get fib 6, gives us fib 1 - 5 as well
recursion calcs,
but doesn&#x2019;t share
---
Here&#x2019;s the Fibrous way of doing it. Again, there is a fundamental concept you need to understand first (co-routines), but I do think this is a slightly more natural way of defining the sequence.
The difference is that to get the 6th number, we have to call resume on the fiber 6 times. With the side-effect of being provided with all the preceding 5 numbers in the sequence.
lazy eval = fibers!
most use I think
is where used in 1.9 stdlib
.each, .map &c without block = enumerator
can be chained
under the hood all done with fibers
--
This sort of lazy evalutation is where Fibers shine, and probably where they&#x2019;ll see the most use.
And, in fact, it&#x2019;s exactly this sort of thing that Fibers are being used for in the ruby 1.9 stdlib. Things like .each and .map have been reworked so that without a block they now return enumerators that you can chain together. And under the hood these enumerators are implemented using fibers.
and in the real world?
(I dunno)
github search
plenty results
on closer inspection
most forks/copies of rubyspec for fibers
(a good resource to read
if you want
know ruby)
the first non-rubyspec result though...
---
So, that&#x2019;s all a bit theoretical. What real use are fibers?
Well, I don&#x2019;t know, so I did a quick search on github, and to my surprise there were actually plenty of results.
But... on closer inspection, the first few pages are entirely forks and copies of the Ruby specs for fibers. Which, by the way, I totally recommend reading if you want to get an idea how something in ruby actually works.
The first result that wasn&#x2019;t a ruby spec requires a detour first...
another quick detour
if you&#x2019;ve done it
you know
evented programming is different
example reading a webpage
normal is simple, call a couple of methods
evented - much more complex.
define state recording models
use callback methods
you gain performance & flexibility
but you lose simplicity and familiarity
---
Well.. another quick detour. If you&#x2019;ve ever done any evented programming you&#x2019;ll know that the code is very different looking to normal code.
Here&#x2019;s a simplified example of how to read a webpage. For the normal case it&#x2019;s really simple, you just call a couple of methods.
The evented case, not so much. You have to rely on callback methods and keep some object around to hold the result of those callbacks. What you lose in a simplified API you gain in performance and flexibility, but it&#x2019;s hard to get your head around.
that first non-rubyspec github hit?
Neverblock - fibers + eventmachine + async libs
give you sync style API for async programming
get performance (not flex)
without changing much code
or the what it feels like
just replace blocking libraries with neverblock
not going to cover in detail. 1 more slide!
--
The first non-ruby spec result on github that uses fibers was: Neverblock.
This library uses Fibers, Event Machine and other non-blocking APIs to present you with an API for doing asynchronous programming that looks remarkably synchronous. So you don&#x2019;t have to change your code to get the benefit of asynchronous performance.
I won&#x2019;t go into details (I only have 1 more slide!), but you should check it out if you&#x2019;re interested.
plenty I didn&#x2019;t cover
remaining API
transfer - yield this fiber + resume another fiber in one go
don&#x2019;t go back to caller
others simple enough
lightweight - less mem than same num threads
single core only (all fibers run in same thread)
--
Last slide. There&#x2019;s loads I didn&#x2019;t cover, but I think I got the basics.
3 remaining API methods (apart from resume and yield).
Transfer is like yield, but instead of giving CPU back to the caller, you give it to the Fiber you called transfer on. The other two are simple enough.
Supremely lightweight. Spinning up fibers takes much less memory than Threads, there&#x2019;s a good comparison.
Single core solution really.
I&#x2019;ll put a resource slide up when I post these slides....