Pages 127-8: Multithreading
Listen to this article.
Value: High
Level: Medium
Summary:
If you want parallelism in your code you can split up cooperating tasks within the program, using multiple threads. Thread#new() creates and runs a new thread to execute the instructions given in block. Any arguments passed to Thread.new are passed into the block. A thread shares all global, instance, and local variables that are in existence at the time the thread starts. However, local variables created within a thread's block are truly local to that thread.
Memo:
If you want to pass a variable to a thread block and work with it inside the block, you better create a brand new one. Don't share variables between threads (to say, don't use inside a thread a variable which already exists before Thread#new() is called): if you do it a following thread *could* manipulate it before the previous thread has finished working with it. You would end up with a bug difficult to track down.
Example:
def try_threads
x = Thread.new { sleep 0.1; print "x"; print "y"; print "z" }
a = Thread.new { print "a"; print "b"; sleep 0.2; print "c" }
sleep 0.3
end
try_threads
Reported errata (at 10/17/06 14:17:18 PDT): 1
My suggestions to the author: 2
Doubts: 0
Level: Medium
Summary:
If you want parallelism in your code you can split up cooperating tasks within the program, using multiple threads. Thread#new() creates and runs a new thread to execute the instructions given in block. Any arguments passed to Thread.new are passed into the block. A thread shares all global, instance, and local variables that are in existence at the time the thread starts. However, local variables created within a thread's block are truly local to that thread.
Memo:
If you want to pass a variable to a thread block and work with it inside the block, you better create a brand new one. Don't share variables between threads (to say, don't use inside a thread a variable which already exists before Thread#new() is called): if you do it a following thread *could* manipulate it before the previous thread has finished working with it. You would end up with a bug difficult to track down.
Example:
def try_threads
x = Thread.new { sleep 0.1; print "x"; print "y"; print "z" }
a = Thread.new { print "a"; print "b"; sleep 0.2; print "c" }
sleep 0.3
end
try_threads
Reported errata (at 10/17/06 14:17:18 PDT): 1
- #1941 is a minor typo.
- Running the sample code on page 128 in my IRb I get "Got www.google.com: Moved Temporarily", running it on ruby.ch I get "Got www.google.com: Found", instead of "Got www.google.com: OK "
My suggestions to the author: 2
- On page 127 paragraphs 2, 3 and 4 concern the difference between native threads and Ruby's in-process, implemented threads. Here the author is assuming the reader knows what he's talking about: it's not always true. I suggest to explain it all with much more detail.
- On page 128 the author should explicit the reason behind the order of the results of the code produced, and why this order could be different when run by the reader.
Doubts: 0
0 Comments:
Post a Comment
<< Home