# Green threads in 1.9.\*?

**URL:** <https://www.ruby-forum.com/t/green-threads-in-1-9/221209>\
**Category:** Ruby\
**Created:** [July 4, 2012, 12:57pm UTC](https://www.ruby-forum.com/t/green-threads-in-1-9/221209 "2012-07-04T12:57:16Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [July 4, 2012, 12:57pm UTC](https://www.ruby-forum.com/t/green-threads-in-1-9/221209/1 "2012-07-04T12:57:16Z")

</div>

I am new to Ruby. I am somewhat surprised that I was not able to find  
much info on this question via google or searching of forums. I was  
able to find a similar question posted a while back but no one answered  
it. Perhaps threads aren’t such an important topic in Ruby culture?

Anyway, to the point. I’d like to use green threads (NOT kernel  
threads) in a Ruby program I’m writing. There is no reason for me to  
use kernel threads, and every reason to use green ones.

Are green threads still available? If so, how do I access/use them  
(Thread class says nothing about whether it is green or kernel - I  
suspect kernel)? If not, why not?

If not available, are there any alternatives? I checked into fibers,  
but they are not appropriate for what I want because if an exception  
happens, it happens in the “main thread” I am executing, which I don’t  
want.

Thanks.

---

<div class="post-metadata">

**Author:** ![rex-goxman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@rex-goxman](https://www.ruby-forum.com/u/rex-goxman)\
**Post date:** [July 4, 2012, 1:18pm UTC](https://www.ruby-forum.com/t/green-threads-in-1-9/221209/2 "2012-07-04T13:18:59Z")

</div>

Hi,

As far as I understand, Ruby 1.9 has native threads, but they’re blocked  
by the Global Interpreter Lock.

So they should work like you want them to.

---

<div class="post-metadata">

**Author:** ![rex-goxman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@rex-goxman](https://www.ruby-forum.com/u/rex-goxman)\
**Post date:** [July 4, 2012, 1:26pm UTC](https://www.ruby-forum.com/t/green-threads-in-1-9/221209/3 "2012-07-04T13:26:21Z")

</div>

It depends on the interpreter. This post may help you get the picture:

[http://www.igvita.com/2008/11/13/concurrency-is-a-myth-in-ruby/](http://www.igvita.com/2008/11/13/concurrency-is-a-myth-in-ruby/)

---

<div class="post-metadata">

**Author:** ![rex-goxman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@rex-goxman](https://www.ruby-forum.com/u/rex-goxman)\
**Post date:** [July 4, 2012, 1:52pm UTC](https://www.ruby-forum.com/t/green-threads-in-1-9/221209/4 "2012-07-04T13:52:36Z")

</div>

rex goxman wrote in post #1067385:

> Are you saying that green threads are used regardless of what is  
> happening with kernel threads and the GIL? The link posted by fxn  
> seems to indicate this.

This looks like a mistake. Ruby 1.8 has green threads, Ruby 1.9 and  
JRuby have native threads (though Ruby 1.9 has a GIL).

---

<div class="post-metadata">

**Author:** ![rex-goxman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@rex-goxman](https://www.ruby-forum.com/u/rex-goxman)\
**Post date:** [July 4, 2012, 1:45pm UTC](https://www.ruby-forum.com/t/green-threads-in-1-9/221209/5 "2012-07-04T13:45:05Z")

</div>

@jacques1 and fxn above:

Are you saying that green threads are used regardless of what is  
happening with kernel threads and the GIL? The link posted by fxn  
seems to indicate this.

EDIT: 1.9.x of course.

Thanks.

---

<div class="post-metadata">

**Author:** ![rex-goxman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@rex-goxman](https://www.ruby-forum.com/u/rex-goxman)\
**Post date:** [July 4, 2012, 2:11pm UTC](https://www.ruby-forum.com/t/green-threads-in-1-9/221209/6 "2012-07-04T14:11:56Z")

</div>

On Wed, Jul 4, 2012 at 1:45 PM, rex goxman [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

@jacques1 and fxn above:

> Are you saying that green threads are used regardless of what is  
> happening with kernel threads and the GIL? The thread posted by fxn  
> seems to indicate this.

I don’t know enough to give a definitive answer (or plainly put, to  
answer), but here’s my interpretation of the post:

I believe the green threads in Ilya’s post are the Ruby-land thread  
objects, they are not necessarily green in the sense of who is doing the  
scheduling. This is my interpretation a posteriori because of the way  
the  
JVM is drawn.

They are definitely green threads in MRI 1.8 in the sense that MRI did  
scheduling (you already know that).

Then, behind the scenes, they are mapped to native threads in MRI 1.9  
and  
JRuby. One-to-one. In the case of MRI there’s a GIL. I guess technically  
a  
GIL does not mean that MRI is doing the scheduling, it means MRI is  
holding  
a lock, but the kernel schedules the native thread (because it is  
native).  
Maybe there’s a gray area in the definition of green thread here, I  
don’t  
know.

That is my interpretation, but I don’t know enough to be certain. Please  
someone correct me if that is wrong!

---

<div class="post-metadata">

**Author:** ![rex-goxman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@rex-goxman](https://www.ruby-forum.com/u/rex-goxman)\
**Post date:** [July 4, 2012, 2:30pm UTC](https://www.ruby-forum.com/t/green-threads-in-1-9/221209/7 "2012-07-04T14:30:38Z")

</div>

rex goxman писал 04.07.2012 16:26:

> TCL  
> version of the same above, which I know uses kernel threads. It only  
> spawned a couple heavyweights per second.
> 
> I guess the results are indeterminate? Hopefully someone else will  
> chime in who knows definitively.

Ruby 1.9 uses kernel threads and does not have green threads.  
There is nothing like Erlang’s lightweight processes in Ruby (or, for  
that matter, in most of the languages).

---

<div class="post-metadata">

**Author:** ![rex-goxman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@rex-goxman](https://www.ruby-forum.com/u/rex-goxman)\
**Post date:** [July 4, 2012, 2:42pm UTC](https://www.ruby-forum.com/t/green-threads-in-1-9/221209/8 "2012-07-04T14:42:20Z")

</div>

On Wed, Jul 4, 2012 at 2:30 PM, Peter Z.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

Ruby 1.9 uses kernel threads and does not have green threads.

> There is nothing like Erlang’s lightweight processes in Ruby (or, for  
> that matter, in most of the languages).

That is clear, but the definition of green thread depends on who  
schedules  
them.

Is the GIL acting as a scheduler? Has MRI logic about what to lock next  
therefore scheduling in practice _among the threads in its process_? Or  
is  
my conjecture right that locking is mostly dumb and that ultimately the  
kernel is the scheduler because the thread is native?

---

<div class="post-metadata">

**Author:** ![rex-goxman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@rex-goxman](https://www.ruby-forum.com/u/rex-goxman)\
**Post date:** [July 4, 2012, 2:46pm UTC](https://www.ruby-forum.com/t/green-threads-in-1-9/221209/9 "2012-07-04T14:46:26Z")

</div>

Xavier N. писал 04.07.2012 16:41:

> schedules  
> them.
> 
> Is the GIL acting as a scheduler? Has MRI logic about what to lock  
> next  
> therefore scheduling in practice _among the threads in its process_?  
> Or is  
> my conjecture right that locking is mostly dumb and that ultimately  
> the  
> kernel is the scheduler because the thread is native?

The OP’s problem is not the scheduler itself but the associated system  
resources.  
You can create 20k Erlang processes (“threads”). You cannot create 20k  
pthreads. Just that.

---

<div class="post-metadata">

**Author:** ![rex-goxman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@rex-goxman](https://www.ruby-forum.com/u/rex-goxman)\
**Post date:** [July 4, 2012, 2:26pm UTC](https://www.ruby-forum.com/t/green-threads-in-1-9/221209/10 "2012-07-04T14:26:30Z")

</div>

If jacques1 is right, then there are no green threads, just kernel  
threads, and I’m out of luck (anyone know why they would do away with  
green threads instead of keeping them and also adding kernel threads?).

I did a quick-and-dirty test here (ruby 1.9.3) to try to shed some  
light. My assumption is that green threads should be incredibly cheap  
to create (on the order of at least thousands per second), where as  
kernel threads should be much, much slower.

(1…5000).each {Thread.new{}}

This takes around 12 seconds to execute on my machine. It seems far too  
slow for green threads (I can create hundreds of thousands of green  
processes in erlang per second). However, someone out there correct me  
if I am wrong, but I would also expect kernel threads to be much slower  
than the above, i.e. on the order of a few per second. I coded up a TCL  
version of the same above, which I know uses kernel threads. It only  
spawned a couple heavyweights per second.

I guess the results are indeterminate? Hopefully someone else will  
chime in who knows definitively.

---

<div class="post-metadata">

**Author:** ![rex-goxman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@rex-goxman](https://www.ruby-forum.com/u/rex-goxman)\
**Post date:** [July 4, 2012, 4:56pm UTC](https://www.ruby-forum.com/t/green-threads-in-1-9/221209/11 "2012-07-04T16:56:59Z")

</div>

You could try to emulate green threads using a native thread pool +  
fibers.

---

<div class="post-metadata">

**Author:** ![rex-goxman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@rex-goxman](https://www.ruby-forum.com/u/rex-goxman)\
**Post date:** [July 4, 2012, 3:00pm UTC](https://www.ruby-forum.com/t/green-threads-in-1-9/221209/12 "2012-07-04T15:00:27Z")

</div>

On Wed, Jul 4, 2012 at 2:45 PM, Peter Z.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

Xavier N. писал 04.07.2012 16:41:

> > 
> 
> resources.  
> You can create 20k Erlang processes (“threads”). You cannot create 20k  
> pthreads. Just that.

Looks so also to me, but Ilya’s post makes that clear (and it is a known  
characteristic of MRI 1.9)… why the followup then after I linked to  
the  
post?

---

<div class="post-metadata">

**Author:** ![rex-goxman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@rex-goxman](https://www.ruby-forum.com/u/rex-goxman)\
**Post date:** [July 4, 2012, 6:13pm UTC](https://www.ruby-forum.com/t/green-threads-in-1-9/221209/13 "2012-07-04T18:13:27Z")

</div>

Peter Z. wrote in post #1067393:

> There is nothing like Erlang’s lightweight processes in Ruby (or, for  
> that matter, in most of the languages).

Well, for the purposes here, if Ruby’s threads were green threads, yes  
they would have been comparable to Erlang green processes. My emphasis  
is far more on the “green” and far less on whether it is a process or a  
thread.

As far as the definition of green and whether it has anything to do with  
a scheduler or not, my definition of green is simply whether or not it  
is a ‘real’ thread or simply a virtual thread which lives inside of the  
vm.

---

<div class="post-metadata">

**Author:** ![rex-goxman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@rex-goxman](https://www.ruby-forum.com/u/rex-goxman)\
**Post date:** [July 4, 2012, 5:19pm UTC](https://www.ruby-forum.com/t/green-threads-in-1-9/221209/14 "2012-07-04T17:19:37Z")

</div>

You might also want to look into tarcieri’s celluloid:

> **[GitHub - celluloid/celluloid: Actor-based concurrent object framework for Ruby](https://github.com/celluloid/celluloid/)**
>
> Actor-based concurrent object framework for Ruby. Contribute to celluloid/celluloid development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![rex-goxman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@rex-goxman](https://www.ruby-forum.com/u/rex-goxman)\
**Post date:** [July 4, 2012, 6:24pm UTC](https://www.ruby-forum.com/t/green-threads-in-1-9/221209/15 "2012-07-04T18:24:02Z")

</div>

rex goxman wrote in post #1067433:

> Well, for the purposes here, if Ruby’s threads were green threads, yes  
> they would have been comparable to Erlang green processes. My emphasis  
> is far more on the “green” and far less on whether it is a process or a  
> thread.

Ruby 1.8 _has_ green threads. It’s explained in Xavier’s link, and I  
also mentioned it above.

You seem to misread “some implementations have native threads” as “all  
have native threads”.

---

<div class="post-metadata">

**Author:** ![rex-goxman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@rex-goxman](https://www.ruby-forum.com/u/rex-goxman)\
**Post date:** [July 4, 2012, 7:03pm UTC](https://www.ruby-forum.com/t/green-threads-in-1-9/221209/16 "2012-07-04T19:03:11Z")

</div>

Jan E. wrote in post #1067435:

> rex goxman wrote in post #1067433:
> 
> > Well, for the purposes here, if Ruby’s threads were green threads, yes  
> > they would have been comparable to Erlang green processes. My emphasis  
> > is far more on the “green” and far less on whether it is a process or a  
> > thread.
> 
> Ruby 1.8 _has_ green threads. It’s explained in Xavier’s link, and I  
> also mentioned it above.
> 
> You seem to misread “some implementations have native threads” as “all  
> have native threads”.

The title of this post is “green threads in 1.9.\* ?” Furthermore, I  
have this edit in one of my posts here:

> EDIT: 1.9.x of course.

So I have misread nothing. I am **ONLY** talking about 1.9.\*. I could  
care less about 1.8 or any other implementation.

Thanks.

---

<div class="post-metadata">

**Author:** ![rex-goxman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@rex-goxman](https://www.ruby-forum.com/u/rex-goxman)\
**Post date:** [July 5, 2012, 3:31am UTC](https://www.ruby-forum.com/t/green-threads-in-1-9/221209/17 "2012-07-05T03:31:17Z")

</div>

On Wed, Jul 4, 2012 at 3:57 AM, rex goxman [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’d like to use green threads (NOT kernel threads) in a Ruby program I’m  
> writing. There is no reason for me to  
> use kernel threads, and every reason to use green ones.

If you want to use 1.9, you’re out of luck. That said, you aren’t  
stating  
your reasoning for why you would prefer green threads over native ones.

Native threads have a longer creation time and use marginally more  
memory  
(they use ~20kB per thread last I measured), however they generally  
perform  
better, particularly around I/O. On platforms like Linux scheduling is  
O(1)  
so there’s really no reason you can’t create large numbers of threads.

There are definitely many reasons to prefer native threads over green  
threads. This is the reason why you have seen a shift from green threads  
to  
native threads in MRI, Rubinius, and the JVM.

Why do you want green threads?

---

<div class="post-metadata">

**Author:** ![rex-goxman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@rex-goxman](https://www.ruby-forum.com/u/rex-goxman)\
**Post date:** [July 5, 2012, 1:40am UTC](https://www.ruby-forum.com/t/green-threads-in-1-9/221209/18 "2012-07-05T01:40:18Z")

</div>

On 5 July 2012 03:03, rex goxman [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> So I have misread nothing. I am **ONLY** talking about 1.9.\*. I could  
> care less about 1.8 or any other implementation.

Couldn’t. Unless your level of caring is greater than your minimum. Or  
you can have negative carativitiy.

I’m pretty sure Xavier (fxn) has answered this thread pretty well. My  
question is: do you really care about the greenness of the threads  
(academically), or do you actually want to know how best to utilise  
them since they seem to have unusual profiling characteristics?

If the former, I don’t know what to suggest. Research, code scouring,  
reverse engineering (or read the link posted earlier). If the latter,  
I say explore and research, and if you find something that works best  
for your application use it, and maybe share it with us in case we  
need to do it again some day.

As an aside, and I’m not pointing the finger at anyone in this thread  
when I say this, but a week or two ago I commented to a colleague of  
mine about how polite the ruby-talk forum was, and how people seemed  
to consciously avoid flaming and trolling, and how noobs got useful  
help even when they asked “dumb noob” questions, and how people said  
“thank you” . . . but in the past couple of days there’ve been a few  
discussions (usually related to optimisation or performance) that have  
developed a very negative tone, if not outright ALL CAPS TYPING and  
rage. What’s going on? I want a return to the happy, friendly  
ruby-talk of yesterweek.

–  
Matthew K., B.Sc (CompSci) (Hons)  
[http://matthew.kerwin.net.au/](http://matthew.kerwin.net.au/)  
ABN: 59-013-727-651

“You’ll never find a programming language that frees  
you from the burden of clarifying your ideas.” - xkcd

---

<div class="post-metadata">

**Author:** ![rex-goxman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@rex-goxman](https://www.ruby-forum.com/u/rex-goxman)\
**Post date:** [July 5, 2012, 3:34am UTC](https://www.ruby-forum.com/t/green-threads-in-1-9/221209/19 "2012-07-05T03:34:13Z")

</div>

On Wed, Jul 4, 2012 at 5:11 AM, Xavier N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Then, behind the scenes, they are mapped to native threads in MRI 1.9 and  
> JRuby. One-to-one. In the case of MRI there’s a GIL. I guess technically a  
> GIL does not mean that MRI is doing the scheduling, it means MRI is holding  
> a lock, but the kernel schedules the native thread (because it is native).  
> Maybe there’s a gray area in the definition of green thread here, I don’t  
> know.

MRI is still doing the scheduling in 1.9. It runs a timer thread that  
interrupts running threads after a certain amount of time so waiting  
threads can run. This way a single thread doing some computationally  
intensive operation doesn’t monopolize the entire VM.

---

<div class="post-metadata">

**Author:** ![rex-goxman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@rex-goxman](https://www.ruby-forum.com/u/rex-goxman)\
**Post date:** [July 5, 2012, 3:36pm UTC](https://www.ruby-forum.com/t/green-threads-in-1-9/221209/20 "2012-07-05T15:36:40Z")

</div>

\<\<There are definitely many reasons to prefer native threads over green  
threads. This is the reason why you have seen a shift from green threads  
to  
native threads in MRI, Rubinius, and the JVM.

Why do you want green threads?\>\>

There are many situations to prefer green threads over kernel  
threads. It’s why I’m somewhat surprised that Ruby is just ditching  
green threads, vs. keeping them and deploying kernel threads along side  
them. Perhaps it’s a cultural thing. Rubyists might be more concerned  
with things like web servers (Rails development) and what not, and  
perhaps haven’t found want or need for green threads so much (just a  
guess).

One reason to prefer green threads is if your application doesn’t need  
speed up through concurrency/parallelism, rather you want a threaded  
solution for algorithmic purposes, convenience purposes, or other  
purposes. This is a crappy example, but say you need to do some  
calculation which might fail or even crash, but you don’t want (or need)  
to check the inputs to the calculation, or wrap the thing up in an error  
catching mechanism, or anything of this nature. You just want to  
calculate it, and if it works, fine, and if it doesn’t (or even crashes)

- fine, your main program continues to run. So spawn a green thread to  
run the calculation. If it works, great. If it crashes, who cares?

You might say “do that with a kernel thread.” But what if I want to  
spawn this thread over and over again in a tight loop? I don’t want or  
need the monstrous overhead of a kernel thread here. I just want a  
green thread.

Again, there’s a whole host of situations where one might want to have  
green threads vs. kernel threads. But I wasn’t here to debate that - if  
Rubyists want to throw green threads in the garbage, that’s their  
prerogative. I was just here to ask whether there were green threads or  
not (in 1.9.\*).

[Next page](https://www.ruby-forum.com/t/green-threads-in-1-9/221209.md?page=2)
