ɮ罸



åɤƱΰĤǤѿ¸륯饹
ConditionVariable ֥ȤϥåɤԤ碌Ԥ
򥪥֥ȲΤǤ

    mutex = Mutex.new
    cv = ConditionVariable.new

    Thread.start {
        mutex.synchronize {
          ...
          while (郎ʤ)
            cv.wait(m)
          end
          ...
        }
    }
륹åɤǾΤ褦˾郎ޤ wait ᥽åɤ
åɤߤơ¾Υåɤ

    Thread.start {
        mutex.synchronize {
          # ξ
          cv.signal
        }
    }

Ȥơsignal ᥽åɤ wait ¹ԤƤ륹åɤФ
ƾ郎ΩȤΤΤŵŪʻǤ
ʲ Pthread ץߥ󥰡פˤä ConditionVariable 
Ruby ǽ񤤤ƤߤʤǸ˥ǥåɥå롣
  require 'thread'

  count_lock = Mutex.new
  count_hit_threshold = ConditionVariable.new
  count = 0
  COUNT_THRESHOLD = 10

  inc_count = proc {
    loop {
      count_lock.synchronize {
          count += 1
          p [Thread.current, count]
          if count >= COUNT_THRESHOLD
            count_hit_threshold.signal
            Thread.exit
          end
      }
    }
  }

  ths = []
  ths << Thread.new(&inc_count)
  ths << Thread.new(&inc_count)
  ths << Thread.new {
    loop {
      count_lock.synchronize {
          if count > 0
            count -= 1
          end
          p [Thread.current, count]
      }
    }
  }
  ths << Thread.new {
    cond_lock.synchronize {
      while (count < COUNT_THRESHOLD)
        count_hit_threshold.wait(count_lock)
        p [Thread.current, count, 'wait']
      end
    }
  }

  ths.each {|t|
    t.join
  }