Tag: 死锁

为什么我会遇到僵局? 使用Thread,Queue和ActiveRecord。 Ruby 1.9.3,Rails 2.3.18

我通过运行多个线程并等待第一个线程以有效答案返回来缓解特定操作的低成功率。 我在下面创建了一个最小的示例: THREADS = [] if !defined?(THREADS) def threadtest THREADS.clear queue, mutex, result = Queue.new, Mutex.new, nil 10.times do |i| THREADS << Thread.new do counted = (10e6 + rand(10e6)).to_i.times { } # randomize computation time num = rand(8) # succeed only 1/8 of the time #mutex.synchronize do # even if wrapped in mutex # print […]

rails中的’deadlock detected’错误

我的代码中检测到死锁错误,并且不明白为什么。 有人可以告诉我,我做错了什么? #!/usr/bin/ruby ENV[‘RAILS_ENV’] = ARGV.first || ENV[‘RAILS_ENV’] || ‘development’ require File.expand_path(File.dirname(__FILE__) + “/config/environment”) mutex = Mutex.new threads = [] 1.upto(10) do |i| threads << Thread.new(i) do |id| mutex.synchronize do # here I want to take 1 record from "class Product < ActiveRecord::Base" Product.first end # and to do here some stuff with it # […]

Ruby join()中的死锁

我在ruby中进行multithreading处理。 代码片段是 threads_array = Array.new(num_of_threads) 1.upto(num_of_threads) do |i| Thread.abort_on_exception = true threads_array[i-1] = Thread.new { catch(:exit) do print “s #{i}” user_id = nil loop do user_id = user_ids.pop() if user_id == nil print “a #{i}” Thread.stop() end dosomething(user_id) end end } end #puts “after thread” threads_array.each {|thread| thread.join} 我没有使用任何互斥锁。 但我得到了死锁..以下是上面代码片段的输出.. s 2s 6s 8s 1s […]

ThreadPool中的死锁

我找不到适合Ruby的ThreadPool实现,所以我写了我的(部分基于这里的代码: http : //web.archive.org/web/20081204101031/http : //snippets.dzone.com : 80/ posts / show / 3276 ,但更改为等待/信号和ThreadPool关闭的其他实现。但是经过一段时间的运行(有100个线程并处理大约1300个任务),它在第25行死机 – 它等待一个新的工作那里。任何想法,为什么会发生? require ‘thread’ begin require ‘fastthread’ rescue LoadError $stderr.puts “Using the ruby-core thread implementation” end class ThreadPool class Worker def initialize(callback) @mutex = Mutex.new @cv = ConditionVariable.new @callback = callback @mutex.synchronize {@running = true} @thread = Thread.new do while @mutex.synchronize […]