Permalink
Browse files

initial commit

  • Loading branch information...
1 parent 9f539c2 commit dd187b700c3621d4d96d469f8831cac99001155e @cocoa committed Jan 8, 2013
Showing with 165 additions and 1 deletion.
  1. +16 −1 README.md
  2. +34 −0 condition_variables.rb
  3. +20 −0 creating_ruby_threads.rb
  4. +18 −0 ensure_with_return.rb
  5. +21 −0 ensure_without_return.rb
  6. +31 −0 the_mutex_class.rb
  7. +13 −0 threads_and_exceptions.rb
  8. +12 −0 threads_without_sync.rb
View
@@ -1,4 +1,19 @@
multithreading-basics
=====================
-multithread basic examples
+multithread basic examples
+
+
+```
+.
+├── README.md
+├── condition_variables.rb
+├── creating_ruby_threads.rb
+├── ensure_with_return.rb
+├── ensure_without_return.rb
+├── the_mutex_class.rb
+├── threads_and_exceptions.rb
+└── threads_without_sync.rb
+
+0 directories, 8 files
+```
@@ -0,0 +1,34 @@
+# A condition variable is simply a semaphore that is associated with a resource and
+# is used within the protection of a particular mutex. When you need a resource that's
+# unavailable, you wait on a condition variable. That action releases the lock on the
+# corresponding mutex. When some other thread signals that the resource is available,
+# the original thread comes off the wait and simultaneously regains the lock on the
+# critical region.
+
+require 'thread'
+mutex = Mutex.new
+cv = ConditionVariable.new
+
+a = Thread.new do
+ mutex.synchronize do
+ p "A: I have critical section, but will wait for cv"
+ cv.wait(mutex)
+ p "A: I have critical section again! I rule!"
+ end
+end
+
+sleep 1
+p '(Later, back at the ranch...)'
+
+
+b = Thread.new do
+ mutex.synchronize do
+ p "B: Now I am critical, but am done with cv"
+ cv.signal
+ p "B: I am still critical, finishing up"
+ end
+end
+
+
+a.join
+b.join
@@ -0,0 +1,20 @@
+require 'net/http'
+
+pages = %w( http://www.dell.com
+ http://www.apple.com
+ http://www.google.co.uk
+ http://www.nauta.com
+ http://www.engadget.com
+ http://www.yahoo.com )
+
+threads = []
+
+pages.each do |page|
+ threads << Thread.new(page) do |uri|
+ p "Fetching: #{page}..."
+ resp = Net::HTTP.get_response(URI(uri))
+ p "Got #{page}: #{resp.message}"
+ end
+end
+
+threads.each{|t| t.join}
View
@@ -0,0 +1,18 @@
+def ensure_without_return
+ yield
+ensure
+ puts 'ensure'
+ return true
+end
+
+ensure_without_return { puts 'block'; false }
+#
+#block
+#ensure
+#=> true
+#
+
+ensure_without_return { raise 'exception raised'; puts 'block'; false }
+#
+#ensure
+#=> true
@@ -0,0 +1,21 @@
+def ensure_without_return
+ yield
+ensure
+ puts 'ensure'
+ true
+end
+
+ensure_without_return { puts 'block'; false }
+#
+#block
+#ensure
+#=> false
+#
+
+ensure_without_return { raise 'exception raised'; puts 'block'; false }
+#
+#ensure
+#RuntimeError: exception raised
+# from (irb):21
+# from (irb):16:in `ensure_without_return'
+# from (irb):21
View
@@ -0,0 +1,31 @@
+require 'thread'
+mutex = Mutex.new
+
+
+count1 = count2 = 0
+difference = 0
+
+counter = Thread.new do
+ loop do
+ mutex.synchronize do
+ count1 += 1
+ count2 += 1
+ end
+ end
+end
+
+spy = Thread.new do
+ loop do
+ mutex.synchronize do
+ difference += (count1 - count2).abs
+ end
+ end
+end
+
+sleep 1
+
+mutex.lock
+
+p count1
+p count2
+p difference
@@ -0,0 +1,13 @@
+Thread.abort_on_exception = false
+
+
+threads = []
+
+6.times { |i|
+ threads << Thread.new(i) {
+ raise "Boom!" if i == 3
+ puts i
+ }
+}
+
+threads.each{|t| t.join}
@@ -0,0 +1,12 @@
+count = 0
+arr = []
+10.times do |i|
+ arr[i] = Thread.new {
+ sleep(rand(0)/10.0)
+ Thread.current["mycount"] = count
+ count += 1
+ }
+end
+
+arr.each {|t| t.join; print t["mycount"], ', '}
+puts "count = #{count}"

0 comments on commit dd187b7

Please sign in to comment.