37. classThreadWatch definitialize(timer) @update_time = nil @alive_timer = timer @mutex = Mutex.new end defupdate @mutex.synchronizedo @update_time = Time.now end end def valid? @mutex.synchronizedo returnfalseif!@update_time.nil? && (Time.now - @update_time) > @alive_timer) end true end end
38. defself.thread_main(idx, watch) loop do watch.update #スレッド処理 end end defself.main(argv) th_infos = Hash.new foriin0…100 w = ThreadWatch.new(600) th = Thread.new(i, w) do |idx, watch| thread_main(idx, watch) end th_infos[i] =[th, w] end check_threads(th_infos) end
39. defself.check_threads(th_infos) loop do th_infos.eachdo |i, info| th, w = info f_restart = false caseth.status when“run”, “sleep” f_restart = true unless w.valid? else f_restart = true end iff_restart th.kill th.join th = Thread.new(i, w) do |idx, watch| thread_main(idx, watch) end th_infos[i] = [th, w] end end end end
40. Beginning Over file descriptor limit root user timeout select() thread stuck own monitoring function.