Threads in Ruby (Basics)

456 views

Published on

Short PPT on how to implement threads in Rails.

Referred links:
1. http://www.sitepoint.com/threads-ruby/
2. http://blog.carbonfive.com/2011/10/11/a-modern-guide-to-threads/

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
456
On SlideShare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Threads in Ruby (Basics)

  1. 1. Threads in Ruby
  2. 2. What is a Thread? • A thread is a light-weight process. • Threads that belong to the same process share that process’s resources.
  3. 3. Threads in Ruby
  4. 4. Threads in Ruby Example 1 def add(arr) sleep(2) sum = 0 arr.each { |item| sum += item } sum end @arr1 = [1, 2, 3] @arr2 = [4, 5, 6] @arr3 = [7, 8, 9] puts "arr1 = #{add(@arr1)}" puts "arr2 = #{add(@arr2)}" puts "arr3 = #{add(@arr3)}"
  5. 5. Threads in Ruby Output : varun@kiprosh-varun:~/projects/test$ time ruby threads.rb arr1 = 6 arr2 = 15 arr3 = 24 real 0m6.039s user0m0.032s sys 0m0.004s
  6. 6. Threads in Ruby Example 2 def add(arr) sleep(2) sum = 0 arr.each { |item| sum += item } sum end @arr1 = [1, 2, 3] @arr2 = [4, 5, 6] @arr3 = [7, 8, 9] threads = (1..3).map do |i| Thread.new(i) do |i| arr = instance_variable_get("@arr#{i}") puts "arr#{i} = #{add(arr)}" end end threads.each { |t| t.join }
  7. 7. Threads in Ruby Output : varun@kiprosh-varun:~/projects/test$ time ruby test.rb arr1 = 6 arr2 = 15 arr3 = 24 real 0m2.035s user0m0.032s sys 0m0.000s varun@kiprosh-varun:~/projects/test$ time ruby test.rb arr2 = 15 arr3 = 24 arr1 = 6 real 0m2.038s user0m0.020s sys 0m0.020s
  8. 8. Race Condition
  9. 9. Race Condition Example 3 class Item class << self; attr_accessor :price end @price = 0 end (1..10).each { Item.price += 10 } puts "Item.price = #{Item.price}“ Output : varun@kiprosh-varun:~/projects/test$ ruby test.rb Item.price = 100
  10. 10. Race Condition Example 4 class Item class << self; attr_accessor :price end @price = 0 end threads = (1..10).map do |i| Thread.new(i) do |i| item_price = Item.price # Reading value sleep(rand(0..2)) item_price += 10 # Updating value sleep(rand(0..2)) Item.price = item_price # Writing value end end threads.each {|t| t.join} puts "Item.price = #{Item.price}“
  11. 11. Race Condition Output : varun@kiprosh-varun:~/projects/test$ ruby test.rb Item.price = 20 varun@kiprosh-varun:~/projects/test$ ruby test.rb Item.price = 30 varun@kiprosh-varun:~/projects/test$ ruby test.rb Item.price = 10
  12. 12. Race Condition The race condition is fundamentally due to the multi-step process of changing a variable. register = i # read the current value from RAM into a register register = register + 1 # increment it by one i = register # write the value back to the variable in RAM ● OS can stop Thread 1 and start executing Thread 2 at any point in time.
  13. 13. Race Condition i=0 # OS is running Thread 1 register = i # 0 register = register + 1 # 1 # OS switches to Thread 2 register = i # 0 register = register + 1 # 1 i = register # 1 # Now OS switches back to Thread 1 i = register # 1
  14. 14. MUTEX (Mutual Exclusion)
  15. 15. Mutual Exclusion class Item class << self; attr_accessor :price end @price = 0 end mutex = Mutex.new threads = (1..10).map do |i| Thread.new(i) do |i| mutex.synchronize do item_price = Item.price # Reading value sleep(rand(0..2)) item_price += 10 # Updating value sleep(rand(0..2)) Item.price = item_price # Writing value end end end threads.each {|t| t.join} puts "Item.price = #{Item.price}"
  16. 16. Mutual Exclusion Output : varun@kiprosh-varun:~/projects/test$ ruby test.rb Item.price = 100 varun@kiprosh-varun:~/projects/test$ ruby test.rb Item.price = 100
  17. 17. Conclusion ● avoid Thread.new – locks exponentially grow the complexity of codebase ● Celluloid or girl_friday – better alternatives than mutex ● JRuby, Rubinius

×