1

我正在使用 ruby​​ 192 p290:在一个单元测试脚本(如下所示)下抛出 ThreadError

1) 错误:

test_orchpr_pass(TC_MyTest):

线程错误:死锁;递归锁定

内部:前奏:8:在“锁定”中

内部:前奏:8:在“同步”中

testth.rb:121:in `orchpr_run'

testth.rb:158:in `test_orchpr_pass'

使用 ruby​​ 187 给出错误:线程试图加入自身。

代码

def orchpr_run(timeout = 60)


    # used by the update function to signal that a final update was
    # received from all clients

    @update_mutex.lock

    # required since we'll have to act as an observer to the DRb server
    DRb.start_service

    # get configuration objects
    run_config_type = DataLayer.get_run_config_type
    client_daemon = DataLayer.get_client_daemon_by_branch (run_config_type, @branch)
            client_daemon['port_no'] = 9096
            #get the servers for this client_daemon
    servers = DataLayer.get_servers(run_config_type, client_daemon.id)

    servers.each { |server| @pr[server.host_name] = OrchestratedPlatformRun.new(run_config_type, server, timeout) 

    }

    @pr.each_value { |x| x.add_observer(self) 

    @pr.each_value { |x| x.start(@service_command_pass, true) 


    # wait for update to receive notifications from all servers # this is the statement causing error:
    @update_mutex.synchronize {} end

另一段代码抛出相同的错误:

 require "thread"
 require "timeout"

 def calc_fib(n)
   if n == 0
     0
   elsif n == 1
     1
   else
     calc_fib(n-1) + calc_fib(n-2)
   end
 end

 lock = Mutex.new

 threads = 20.times.collect do
   Thread.new do
     20.times do
       begin
         Timeout.timeout(0.25) do
           lock.synchronize{ calc_fib(1000) }
         end
       rescue ThreadError => e
         puts "#{e.class}: #{e.message}:\n" + e.backtrace.join("\n") + "\n\n"
       rescue Timeout::Error => e
         #puts e.class
         nil
       end
     end
   end
 end

 threads.each{ |t| t.join }

注释同步块将导致错误消失,但随后线程无法同步。我在网上找到了一些东西,说 ruby​​ 192 的错误,需要在文件prelude.rbthread.c中更改有关 MUTEX 同步的内容。但是在windows下安装找不到文件prelude.rb

4

1 回答 1

1

如果互斥锁被线程锁定,那么如果您尝试从同一个线程再次锁定它,则会引发错误

这正是您正在做的事情,因为synchronize这只是锁定互斥锁、让步到块然后释放锁的方法的一种方便。我不确定您要做什么,但在我看来,您可能正在尝试将互斥锁用于其预期目的以外的其他用途。

很难正确使用线程和锁——您可能想查看赛璐珞以获得不同的并发方法。

于 2013-05-23T12:49:59.673 回答