Threads in Ruby (Basics)

50 %
50 %
Information about Threads in Ruby (Basics)
Technology

Published on February 17, 2014

Author: varunlalan

Source: slideshare.net

Description

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/

Threads in Ruby

What is a Thread? • A thread is a light-weight process. • Threads that belong to the same process share that process’s resources.

Threads in Ruby

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)}"

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

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 }

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

Race Condition

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

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}“

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

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.

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

MUTEX (Mutual Exclusion)

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}"

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

Conclusion ● avoid Thread.new – locks exponentially grow the complexity of codebase ● Celluloid or girl_friday – better alternatives than mutex ● JRuby, Rubinius

Add a comment

Related presentations

Related pages

Threads in Ruby

Let’s see how threads can be useful to us. Basic Example. Consider the following code. def calculate_sum ... Even though have native threads in Ruby 1.9, ...
Read more

Ruby - Multithreading - Tutorials for MFC, SAP HR, Yii ...

Ruby Multithreading - Learning Ruby in simple and easy steps ... Ruby Basics; Ruby - Home; Ruby ... Ruby threads are a lightweight and efficient way to ...
Read more

Ruby Thread Basics

t = Thread. new. When you run this program, you get: ThreadError: must be called with a block Let's use a block when we create the thread: t = Thread. new ...
Read more

Ruby Socket Programming and Threads: Ruby Study Notes ...

Ruby Socket Programming and Threads: Ruby Study Notes - Best Ruby Tutorial. ... Basic Networking. Let us talk a little bit about basic networking.
Read more

Basic Ruby Thread Usage - YouTube

c# (Csharp) threading interview question:- What is thread,background thread and foreground thread ? - Duration: 12:16. .NET Interview ...
Read more

Ruby Blue Quilting Studio: Back to Basics - Thread

Glazed Quilting Thread (Hand Quilting Thread) - To my knowledge this thread usually only comes in one weight and is intended only for hand quilting.
Read more

java - "implements Runnable" vs. "extends Thread" - Stack ...

From what time I've spent with threads in Java, ... “implements Runnable” vs. “extends Thread ... Can we re-visit the basic reason we wanted our ...
Read more

Lesson: Concurrency (The Java™ Tutorials > Essential ...

Lesson: Concurrency. ... This lesson introduces the platform's basic concurrency support and summarizes some of the high ... Processes and Threads ...
Read more

Python Multithreaded Programming - Tutorials for MFC, SAP ...

Python Multithreaded Programming ... Basic Syntax; Python - Variable Types; ... Running several threads is similar to running several different programs ...
Read more