[#927] UnboundMethod#to_proc — Dave Thomas <dave@...>

I'm wondering what I can do with a Proc generated by

17 messages 2003/04/06

Creating thread safe classes for Ruby

From: Konstantin Knizhnik <knizhnik@...>
Date: 2003-04-08 17:31:02 UTC
List: ruby-core #951
Hello,

I want make my class correctly executed in multithreaded environment.
That is why I am using mutex class to synchronize access to the
object:

require "thread"
class Foo
  def initialize()
      @mutex = Mutex.new
  end

  def someCriticalMethod()
     @mutex.synchronize {
        ... do something
     }
  end
end

But if I then invoke this method outside any thread I will get the
following error:

C:/ruby-180/lib/ruby/1.8/thread.rb:94:in `stop': stopping only thread (ThreadError)
        note: use sleep to stop forever
        from C:/ruby-180/lib/ruby/1.8/thread.rb:94:in `lock'
        from C:/ruby-180/lib/ruby/1.8/thread.rb:127:in `synchronize'
        ...


Is there some correct way in Ruby to write thread safe code which will
be also executed outside thread context?

-- 
Best regards,
 Konstantin                          mailto:knizhnik@garret.ru


In This Thread

Prev Next