`synchronize': stopping only thread

  • Follow


Hi.

I am having trouble with threading in Ruby.
My code outputs: `synchronize': stopping only thread (ThreadError).

What does this mean and what could the cause of this error be?!
I have a method named p, defined as follows:

      @mutex.synchronize do
        db_open
        yield(@db)
        db_close
      end

...p gets called with a block which uses db.

Cheers,

Sergio
-- 
Posted via http://www.ruby-forum.com/.

0
Reply Sergio 9/9/2010 1:22:29 PM

On Sep 9, 2010, at 09:22, Sergio Lombrico wrote:
> I am having trouble with threading in Ruby.
> My code outputs: `synchronize': stopping only thread (ThreadError).
>
> What does this mean and what could the cause of this error be?!

It means that you have a deadlock situation, where one thread is  
waiting for a resource that will never be released.

My ruby (1.8.7) gives a slightly different error when I try this but  
take this example:

mutex = Mutex.new

mutex.synchronize do     # (1) lock the mutex
   mutex.synchronize do   # Wait for the mutex to become available  
then lock it
     puts "hi"
   end
end                      # unlock the mutex locked by (1)

The inner "synchronize" is waiting for the mutex lock, but it will  
never get it, because the mutex will only get unlocked when the outer  
synchronize finishes.

> I have a method named p, defined as follows:
>
>      @mutex.synchronize do
>        db_open
>        yield(@db)
>        db_close
>      end

If the block you're passing into this method tries to lock the mutex,  
either directly or by calling synchronize, you'll get deadlock.  The  
block you pass will be trying to lock the mutex, but the mutex is  
already locked by the 'p' method.  By the way, you might not want to  
call your method 'p' because 'p' is already a well-known, often-used  
method.

Ben


0
Reply Ben 9/9/2010 5:08:51 PM


1 Replies
249 Views

(page loaded in 0.053 seconds)

Similiar Articles:












7/25/2012 4:20:39 AM


Reply: