# \`synchronize': stopping only thread

**URL:** <https://www.ruby-forum.com/t/synchronize-stopping-only-thread/194722>\
**Category:** Ruby\
**Created:** [September 9, 2010, 3:22pm UTC](https://www.ruby-forum.com/t/synchronize-stopping-only-thread/194722 "2010-09-09T15:22:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![malvadomx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@malvadomx](https://www.ruby-forum.com/u/malvadomx)\
**Post date:** [September 9, 2010, 3:22pm UTC](https://www.ruby-forum.com/t/synchronize-stopping-only-thread/194722/1 "2010-09-09T15:22:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![malvadomx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@malvadomx](https://www.ruby-forum.com/u/malvadomx)\
**Post date:** [September 9, 2010, 7:12pm UTC](https://www.ruby-forum.com/t/synchronize-stopping-only-thread/194722/2 "2010-09-09T19:12:48Z")

</div>

On Sep 9, 2010, at 09:22, Sergio L. 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
