Thread fun

By Gentle Hornbill, 4 Days ago, written in ruby.
  1. class Foo
  2.   def initialize
  3.     @a = 3
  4.     @b = 2
  5.   end
  6.  
  7.   def m1
  8.     @a = @b - 1
  9.   end
  10.  
  11.   def m2
  12.     @b = @a + 2
  13.   end
  14.  
  15.   def to_s
  16.     "(#{@a}, #{@b})"
  17.   end
  18. end
  19.  
  20.  
  21. $foo = Foo.new
  22. # Create the two threads
  23. t1 = Thread.new {$foo.m1}
  24. t2 = Thread.new {$foo.m2}
  25.  
  26. # Waits for the threads to stop
  27. t1.join
  28. t2.join
  29.  
  30. # Print out the result
  31. print $foo.to_s
  32.  
  33. exit