タグ

ブックマーク / unageanu.hatenablog.com (1)

  • Mutex - うなの日記

    Thread間の処理の同期にはMutexを使います。 Mutex#synchronizeで Mutexのロックの獲得 ブロックの処理を実行。 ロックの解除 を行います。Javaのsynchronizedブロックと似た感じで使えます。 require 'thread' m = Mutex.new ts = [] 3.times { |j| ts << Thread.start { m.synchronize { # ブロック内の処理が同期実行される。 5.times { |i| puts "thread-" << j.to_s << " : " << i.to_s sleep rand * 0.1 } } } } ts.each {|t| t.join } 実行結果です。ブロック内の処理が同期実行されています。 thread-0 : 0 thread-0 : 1 thread-0 : 2 t

    Mutex - うなの日記
  • 1