# prac0703.rb require 'thread' memory = "10100101011010" cs = Mutex::new Thread::fork{ # 表示スレッド before = "" loop{ cs.synchronize{ if before != memory puts memory before = memory.dup end } } } Thread::fork{ # 書き換えスレッド1 loop{ sleep(0.5) cs.synchronize{ memory.gsub!(/001/,"000") } } } Thread::fork{ # 書き換えスレッド2 loop{ sleep(0.5) cs.synchronize{ memory.gsub!(/010/,"000") } } } Thread::fork{ # 書き換えスレッド3 loop{ sleep(0.5) cs.synchronize{ memory.gsub!(/100/,"000") } } } Thread::fork{ # 書き換えスレッド4 loop{ sleep(0.5) cs.synchronize{ memory.gsub!(/110/,"111") } } } Thread::fork{ # 書き換えスレッド5 loop{ sleep(0.5) cs.synchronize{ memory.gsub!(/101/,"111") } } } Thread::fork{ # 書き換えスレッド6 loop{ sleep(0.5) cs.synchronize{ memory.gsub!(/011/,"111") } } } loop{ # 親スレッドは無限ループ }