Sha256: ee36761d31def3a9d8d8a0295f3a0b97f694b34d70a3df16c751fb6f306cad4a
Contents?: true
Size: 1.1 KB
Versions: 16
Compression:
Stored size: 1.1 KB
Contents
require 'spec' dir = File.dirname __FILE__ require "#{dir}/spec_require" require "#{dir}/../lib/ruby_ext/synchronizer" module RubyExt describe "Synchronize" do it "synchronize" do class Account inherit Synchronizer attr_reader :from, :to def initialize super @from, @to = 0, 0 end def transfer @from -= 1 @to += 1 end synchronize :transfer end a, threads = Account.new, [] 100.times do t = Thread.new do 100.times{a.transfer} end threads << t end threads.each{|t| t.join} a.from.should == -10_000 a.to.should == 10_000 end it "synchronize_all" do class Account inherit Synchronizer attr_reader :from, :to def initialize super @from, @to = 0, 0 end def transfer @from -= 1 @to += 1 end synchronize_all end a, threads = Account.new, [] 100.times do t = Thread.new do 100.times{a.transfer} end threads << t end threads.each{|t| t.join} a.from.should == -10_000 a.to.should == 10_000 end end end
Version data entries
16 entries across 16 versions & 1 rubygems