# Ruby and threading

**URL:** <https://www.ruby-forum.com/t/ruby-and-threading/212866>\
**Category:** Ruby\
**Created:** [October 16, 2011, 10:51am UTC](https://www.ruby-forum.com/t/ruby-and-threading/212866 "2011-10-16T10:51:34Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [October 16, 2011, 10:51am UTC](https://www.ruby-forum.com/t/ruby-and-threading/212866/1 "2011-10-16T10:51:34Z")

</div>

Hello,

Is the current ruby 1.9.2 multithreaded at the OS level?

Regards,

Carter.

---

<div class="post-metadata">

**Author:** ![Carter\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c77e96/32.png) [@Carter\_C](https://www.ruby-forum.com/u/Carter_C)\
**Post date:** [October 16, 2011, 11:04am UTC](https://www.ruby-forum.com/t/ruby-and-threading/212866/2 "2011-10-16T11:04:31Z")

</div>

On Sun, Oct 16, 2011 at 3:50 AM, Carter C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hello,
> 
> Is the current ruby 1.9.2 multithreaded at the OS level?
> 
> Regards,
> 
> Carter.

For MRI, yes, but it has a global interpreter lock. Here’s a blog that  
explains all the nuances.  
[http://www.engineyard.com/blog/2011/ruby-concurrency-and-you/](http://www.engineyard.com/blog/2011/ruby-concurrency-and-you/)

---

<div class="post-metadata">

**Author:** ![Carter\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c77e96/32.png) [@Carter\_C](https://www.ruby-forum.com/u/Carter_C)\
**Post date:** [October 16, 2011, 1:40pm UTC](https://www.ruby-forum.com/t/ruby-and-threading/212866/3 "2011-10-16T13:40:10Z")

</div>

Hi Josh,

Thanks for the reply. I was looking over the YARV implementation in  
1.9.3rc1  
and did notice some support for pthreads and ruby threads (and some  
description in the comments of certain models). Do you know if (or does  
anyone else) if this code is fully implemented at present? Or is it a  
global  
lock like situation?

Regards,

Carter.

---

<div class="post-metadata">

**Author:** ![Carter\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c77e96/32.png) [@Carter\_C](https://www.ruby-forum.com/u/Carter_C)\
**Post date:** [October 16, 2011, 1:58pm UTC](https://www.ruby-forum.com/t/ruby-and-threading/212866/4 "2011-10-16T13:58:03Z")

</div>

Hey Carter-

MRI will not be removing the GIL any time soon. For true concurrency,  
you should use JRuby or Rubinius.

-Steve

---

<div class="post-metadata">

**Author:** ![Carter\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c77e96/32.png) [@Carter\_C](https://www.ruby-forum.com/u/Carter_C)\
**Post date:** [October 16, 2011, 7:08pm UTC](https://www.ruby-forum.com/t/ruby-and-threading/212866/5 "2011-10-16T19:08:59Z")

</div>

Is there any big projects using jRuby? It seems that jRuby is available  
for a long time, but still has not very much attention.

---

<div class="post-metadata">

**Author:** ![Carter\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c77e96/32.png) [@Carter\_C](https://www.ruby-forum.com/u/Carter_C)\
**Post date:** [October 16, 2011, 9:09pm UTC](https://www.ruby-forum.com/t/ruby-and-threading/212866/6 "2011-10-16T21:09:44Z")

</div>

JRuby is being used at several companies including LinkedIn and Square.  
It  
also powers the HBase console.

---

<div class="post-metadata">

**Author:** ![Carter\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c77e96/32.png) [@Carter\_C](https://www.ruby-forum.com/u/Carter_C)\
**Post date:** [October 16, 2011, 9:38pm UTC](https://www.ruby-forum.com/t/ruby-and-threading/212866/7 "2011-10-16T21:38:45Z")

</div>

I’ve done some consulting work, and JRuby was being used heavily.

---

<div class="post-metadata">

**Author:** ![Carter\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c77e96/32.png) [@Carter\_C](https://www.ruby-forum.com/u/Carter_C)\
**Post date:** [October 16, 2011, 9:54pm UTC](https://www.ruby-forum.com/t/ruby-and-threading/212866/8 "2011-10-16T21:54:12Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA512

On 10/16/11 1:57 PM, Steve K. wrote:

> MRI will not be removing the GIL any time soon. For true  
> concurrency, you should use JRuby or Rubinius.

or MacRuby.

* * *

All the best, Sandor Sz

---

<div class="post-metadata">

**Author:** ![Carter\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c77e96/32.png) [@Carter\_C](https://www.ruby-forum.com/u/Carter_C)\
**Post date:** [October 16, 2011, 10:36pm UTC](https://www.ruby-forum.com/t/ruby-and-threading/212866/9 "2011-10-16T22:36:10Z")

</div>

On Sun, Oct 16, 2011 at 7:59 PM, Josh C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> and  
> then their new portion was written in Rails, and they were coexisting via  
> JRuby.

In London there are a few big name companies following this MO.

They wrap up a legacy Java app with Ruby based acceptance tests  
(cucumber/capybara is popular), and then extend the app with Ruby based  
extensions (no new production code with Java).

JRuby glues up a lot of it - companies have put a lot of investment in a  
Java-based operations backend. JRuby lets them keep it. Its just the JSP  
grunge that they want to be rid of.

---

<div class="post-metadata">

**Author:** ![Carter\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c77e96/32.png) [@Carter\_C](https://www.ruby-forum.com/u/Carter_C)\
**Post date:** [October 17, 2011, 8:47am UTC](https://www.ruby-forum.com/t/ruby-and-threading/212866/10 "2011-10-17T08:47:28Z")

</div>

I gather then the 1.9.2. implementation at present does not “exploit” OS  
level threads (where as JRuby borrows them from the underlying JVM in  
some  
manner)?

---

<div class="post-metadata">

**Author:** ![Carter\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c77e96/32.png) [@Carter\_C](https://www.ruby-forum.com/u/Carter_C)\
**Post date:** [October 16, 2011, 9:01pm UTC](https://www.ruby-forum.com/t/ruby-and-threading/212866/11 "2011-10-16T21:01:03Z")

</div>

On Sun, Oct 16, 2011 at 12:09 PM, Alexey P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Is there any big projects using jRuby? It seems that jRuby is available  
> for a long time, but still has not very much attention.

Obtiva (who is now owned by Groupon) gave a JRuby workshop at Red Dirt  
Ruby  
Conf. The presenters, Tyler J., and Noel Rappin, said that they  
were  
using JRuby on a large client site, which was initially written in Java,  
and  
then their new portion was written in Rails, and they were coexisting  
via  
JRuby.

---

<div class="post-metadata">

**Author:** ![Carter\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c77e96/32.png) [@Carter\_C](https://www.ruby-forum.com/u/Carter_C)\
**Post date:** [October 17, 2011, 6:30pm UTC](https://www.ruby-forum.com/t/ruby-and-threading/212866/12 "2011-10-17T18:30:58Z")

</div>

On Mon, Oct 17, 2011 at 2:47 AM, Carter C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I gather then the 1.9.2. implementation at present does not “exploit” OS  
> level threads (where as JRuby borrows them from the underlying JVM in some  
> manner)?

You gather incorrectly. Ruby 1.9.2 does use underlying OS threads. For  
a variety of reasons, however, there is a Global Interpreter Lock as  
Josh C. pointed out to you (and provided this link:  
[http://www.engineyard.com/blog/2011/ruby-concurrency-and-you/](http://www.engineyard.com/blog/2011/ruby-concurrency-and-you/)).

-a

---

<div class="post-metadata">

**Author:** ![Carter\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c77e96/32.png) [@Carter\_C](https://www.ruby-forum.com/u/Carter_C)\
**Post date:** [October 17, 2011, 10:02pm UTC](https://www.ruby-forum.com/t/ruby-and-threading/212866/13 "2011-10-17T22:02:13Z")

</div>

Maybe it’s a stupid question, but I can’t get it - what’s the point of  
using OS threads with GIL?

You’ll anyway never get situation where two thread runs simultaneously,  
so, what’s the point of it?

---

<div class="post-metadata">

**Author:** ![Carter\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c77e96/32.png) [@Carter\_C](https://www.ruby-forum.com/u/Carter_C)\
**Post date:** [October 17, 2011, 10:15pm UTC](https://www.ruby-forum.com/t/ruby-and-threading/212866/14 "2011-10-17T22:15:28Z")

</div>

On Mon, Oct 17, 2011 at 1:02 PM, Alexey P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Maybe it’s a stupid question, but I can’t get it - what’s the point of  
> using OS threads with GIL?
> 
> You’ll anyway never get situation where two thread runs simultaneously,  
> so, what’s the point of it?

Threads can still run simultaneously, they just can’t make changes to  
Ruby  
objects or the Ruby environment.

A native extension can release the GIL and do blocking I/O or perform a  
complex computation (e.g. crypto) while Ruby code is running in another  
thread.

---

<div class="post-metadata">

**Author:** ![Carter\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c77e96/32.png) [@Carter\_C](https://www.ruby-forum.com/u/Carter_C)\
**Post date:** [October 17, 2011, 10:16pm UTC](https://www.ruby-forum.com/t/ruby-and-threading/212866/15 "2011-10-17T22:16:43Z")

</div>

On Mon, Oct 17, 2011 at 3:02 PM, Alexey P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Maybe it’s a stupid question, but I can’t get it - what’s the point of  
> using OS threads with GIL?
> 
> You’ll anyway never get situation where two thread runs simultaneously,  
> so, what’s the point of it?
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

“MRI 1.9 uses the same technique as MRI 1.8 to improve the situation,  
namely  
the GIL is released if a Thread is waiting on an external event  
(normally  
IO) which improves responsiveness.”

– [http://www.engineyard.com/blog/2011/ruby-concurrency-and-you/](http://www.engineyard.com/blog/2011/ruby-concurrency-and-you/)

---

<div class="post-metadata">

**Author:** ![Carter\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c77e96/32.png) [@Carter\_C](https://www.ruby-forum.com/u/Carter_C)\
**Post date:** [October 17, 2011, 10:17pm UTC](https://www.ruby-forum.com/t/ruby-and-threading/212866/16 "2011-10-17T22:17:19Z")

</div>

On Mon, Oct 17, 2011 at 10:02 PM, Alexey P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Maybe it’s a stupid question, but I can’t get it - what’s the point of  
> using OS threads with GIL?
> 
> You’ll anyway never get situation where two thread runs simultaneously,  
> so, what’s the point of it?

No, with a GIL no two threads can concurrently hold the lock. But a  
thread which does not need the lock can execute in parallel (e.g.  
while doing a syscall). For specifics you would have to ask Matz or  
read the source.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Carter\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c77e96/32.png) [@Carter\_C](https://www.ruby-forum.com/u/Carter_C)\
**Post date:** [October 19, 2011, 9:14am UTC](https://www.ruby-forum.com/t/ruby-and-threading/212866/17 "2011-10-19T09:14:11Z")

</div>

On Sun, Oct 16, 2011 at 4:03 AM, Josh C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> For MRI, yes, but it has a global interpreter lock. Here’s a blog that  
> explains all the nuances.  
> [http://www.engineyard.com/blog/2011/ruby-concurrency-and-you/](http://www.engineyard.com/blog/2011/ruby-concurrency-and-you/)

A great followup to this post, explains why the GIL exists  
[http://merbist.com/2011/10/18/data-safety-and-gil-removal/](http://merbist.com/2011/10/18/data-safety-and-gil-removal/)

When I ran the code Matt provides under MRI 1.9.3 (has GIL) and  
Rubinius,  
JRuby, MacRuby (native threads, no GIL):

$ rvm 1.9.3-rc1,rbx-2.0.0pre,jruby-1.6.4,macruby-0.10 do ruby  
needs\_gil.rb

ruby-1.9.3-rc1  
0.064 seconds  
400000 elements in array (should be 400000)

rbx-2.0.0pre  
0.232 seconds  
398877 elements in array (should be 400000)

jruby-1.6.4  
0.069 seconds  
398709 elements in array (should be 400000)

macruby-0.10  
0.076 seconds  
366231 elements in array (should be 400000)

$ cat needs\_gil.rb  
puts ‘’, ENV[‘RUBY\_VERSION’]

@array, threads = ,   
start = Time.now  
4.times do  
threads \<\< Thread.new { (1..100\_000).each {|n| @array \<\< n} }  
end  
threads.each{|t| t.join }  
stop = Time.now

puts “%0.3f seconds” % (stop - start), @array.size

Note:

- Other times I ran it under Rubinius, the array got corrupted or  
something  
“Tuple::copy\_from: index 8092 out of bounds for size 5395  
(Rubinius::ObjectBoundsExceededError)”
- Other times I ran it under JRuby, it detected the corrupt data with  
‘ConcurrencyError: Detected invalid array contents due to unsynchronized  
modifications with concurrent users’
- I ran this a whole bunch of times, sometimes MRI was fastest,  
sometimes  
MacRuby, sometimes JRuby (MRI was fastest most consistently, though)

Thoughts:

- MRI has a GIL, thus keeping the data safe, and still performs  
equivalently  
with other implementations (for this admittedly limited test), so do  
benchmarks to decide if this will be worthwhile. It’s not a fluke that  
Matz  
wants to keep the GIL.
- I’m glad JRuby notices the corrupt data (though not always) I’m a big  
fan  
of fail-fast
- Has JRuby fixed their startup time issue? I ran this a lot of times  
and  
didn’t notice any of the lag I used to.

---

<div class="post-metadata">

**Author:** ![Carter\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c77e96/32.png) [@Carter\_C](https://www.ruby-forum.com/u/Carter_C)\
**Post date:** [October 19, 2011, 2:57pm UTC](https://www.ruby-forum.com/t/ruby-and-threading/212866/18 "2011-10-19T14:57:06Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA512

On 10/19/11 9:11 AM, Josh C. wrote:

> 1. 
> 
> $ cat needs\_gil.rb puts ‘’, ENV[‘RUBY\_VERSION’]
> 
> @array, threads = , start = Time.now 4.times do threads \<\<  
> Thread.new { (1..100\_000).each {|n| @array \<\< n} } end  
> threads.each{|t| t.join } stop = Time.now
> 
> puts “%0.3f seconds” % (stop - start), @array.size

I think it’s pretty obvious, that implementations without GIL should  
behave exactly as you have shown.  
You did not use synchronization to append an item to your shared  
object, so sometimes items will get lost.

* * *

All the best, Sandor Sz

---

<div class="post-metadata">

**Author:** ![Carter\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c77e96/32.png) [@Carter\_C](https://www.ruby-forum.com/u/Carter_C)\
**Post date:** [October 19, 2011, 3:27pm UTC](https://www.ruby-forum.com/t/ruby-and-threading/212866/19 "2011-10-19T15:27:22Z")

</div>

On Wed, Oct 19, 2011 at 7:56 AM, Sandor Szcs  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> > Thread.new { (1..100\_000).each {|n| @array \<\< n} } end  
> > threads.each{|t| t.join } stop = Time.now
> > 
> > puts “%0.3f seconds” % (stop - start), @array.size
> 
> I think it’s pretty obvious, that implementations without GIL should  
> behave exactly as you have shown.  
> You did not use synchronization to append an item to your shared  
> object, so sometimes items will get lost.
> 
> * * *

That’s the point, to show why the GIL is useful.

---

<div class="post-metadata">

**Author:** ![Carter\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c77e96/32.png) [@Carter\_C](https://www.ruby-forum.com/u/Carter_C)\
**Post date:** [October 19, 2011, 4:38pm UTC](https://www.ruby-forum.com/t/ruby-and-threading/212866/20 "2011-10-19T16:38:34Z")

</div>

It’s not useful, it’s hiding a problem: your code isn’t thead-safe.

[Next page](https://www.ruby-forum.com/t/ruby-and-threading/212866.md?page=2)
