question: threads behaviour

  • Thread starter Raphael Bauduin
  • Start date
R

Raphael Bauduin

Hi,

As ruby threads are so called green threads, what's the behaviour when
one thread is blocked on IO?
If the thread is blocked on a system call, is the whole process, ie
all threads blocked because the kernel scheduler considers the process
as blocked on IO? Are there IOs blocking a thread without blocking the
whole ruby process?

Thanks

Rapha=EBl
 
M

me

Hi,

As ruby threads are so called green threads, what's the behaviour when
one thread is blocked on IO?
If the thread is blocked on a system call, is the whole process, ie
all threads blocked because the kernel scheduler considers the process
as blocked on IO? Are there IOs blocking a thread without blocking the
whole ruby process?

Thanks

Raphaël

I'm new to threading, but from what I've been able to find, if the
currently active thread is blocked on IO, it should block the rest of them.
At least, certain circumstances could cause that. According to (the rather
old) post at http://www.justskins.com/forums/when-threads-block-107159.html
, it's possible to block on IO. My own tests are unable to confirm that,
even for large test sizes. My guess is that in the 7ish years since that
post, many improvements have gone into the interpreter. ;) The post at
http://stackoverflow.com/questions/3481866/thread-join-blocks-the-main-thread
has more information on ways of using them to not block each other.

http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_threads.html was
where I started for threading, but the only references I see to blocking in
it talk about the Thread.critical= method.

http://stackoverflow.com/questions/56087/does-ruby-have-real-multithreading/57802#57802
has a nice synopsis of the threading types between ruby implementations.
 
E

Eric Wong

Raphael Bauduin said:
Hi,

As ruby threads are so called green threads, what's the behaviour when
one thread is blocked on IO?

They're only green threads on Ruby 1.8, 1.9 uses native threads.
If the thread is blocked on a system call, is the whole process, ie
all threads blocked because the kernel scheduler considers the process
as blocked on IO? Are there IOs blocking a thread without blocking the
whole ruby process?

1.8 will schedule other threads if the blocking IO is for a socket/pipe
and uses the normal Ruby IO interfaces (or the extension author was
careful). Filesystem I/O (including NFS) can block the entire process
in 1.8.
 
R

Robert Klemme

They're only green threads on Ruby 1.8, 1.9 uses native threads.


1.8 will schedule other threads if the blocking IO is for a socket/pipe
and uses the normal Ruby IO interfaces (or the extension author was
careful). =A0Filesystem I/O (including NFS) can block the entire process
in 1.8.

Where do you take that from? AFAIK internally Ruby 1.8.* uses non
blocking IO calls in combination with select() to be able to let other
threads work while IO is under way.

So: if IO blocks generally other threads continue to work - even on 1.8.

Cheers

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
E

Eric Wong

Robert Klemme said:
Where do you take that from? AFAIK internally Ruby 1.8.* uses non
blocking IO calls in combination with select() to be able to let other
threads work while IO is under way.

Yes, I meant "blocking" from a user-visible perspective, not from a
kernel perspective. If there's an EAGAIN it'll block the current thread
from a user perspective and schedule others using select(), but not
actually block on the syscall that caused EAGAIN.
 
R

Robert Klemme

Yes, I meant "blocking" from a user-visible perspective, not from a
kernel perspective. If there's an EAGAIN it'll block the current thread
from a user perspective and schedule others using select(), but not
actually block on the syscall that caused EAGAIN.

OK, but earlier you said that file system IO can block the entire
process - which is not exactly true and also does not match what you
said now. :)

Cheers

robert
 
E

Eric Wong

Robert Klemme said:
OK, but earlier you said that file system IO can block the entire
process - which is not exactly true and also does not match what you
said now. :)

Filesystem IO (regular files) doesn't return EAGAIN (at least not on
systems I'm familiar with).
 
E

Eric Wong

Robert Klemme said:

From http://www.kernel.org/doc/man-pages/online/pages/man2/read.2.html#NOTES

Many file systems and disks were considered to be fast enough
that the implementation of O_NONBLOCK was deemed unnecessary.
So, O_NONBLOCK may not be available on files and/or disks.

From what I've seen, setting O_NONBLOCK doesn't error, but read() never
actually respects it on ext{2,3,4} even when a disk is horribly slow.
 
J

Jörg W Mittag

Eric said:
They're only green threads on Ruby 1.8, 1.9 uses native threads.

That's not true. The vast majority of Ruby 1.8 implementations use
native threads, and I think there is at least one Ruby 1.9
implementation which uses green threads.

jwm
 
R

Robert Klemme

That's not true. The vast majority of Ruby 1.8 implementations use
native threads, and I think there is at least one Ruby 1.9
implementation which uses green threads.

Really? Which are those?

Cheers

robert
 
J

Jörg W Mittag

Robert said:
Really? Which are those?

Ruby 1.8 implementations which use native threads:

* Rubinius
* JRuby (uses JVM threads, which are native on most JVMs, certainly
the ones that people *actually* use)
* XRuby (same)
* IronRuby (same, but for CLI threads)
* Ruby.NET (same)
* Cardinal (I think)
* ... probably a few others ...

As for a Ruby 1.9 implementation that uses green threads, I distinctly
remember reading about one, but I forgot which one. There's so many
Ruby implementations right now that it's hard to keep track of which
ones are 1.8 or 1.9 (or both), which ones have native threads or green
threads, which ones have concurrent threads or serialized threads.

(Interestingly, I haven't seen any implementations with concurrent
green threads, which IMO is the best kind of threads. Rubinius
originally planned to do concurrent green threads, but they never did.
They started off with serialized green threads, then switched to
serialized native threads and are currently in the process of
switching to concurrent native threads.)

jwm
 
C

Charles Oliver Nutter

2011/3/5 J=C3=B6rg W Mittag said:
(Interestingly, I haven't seen any implementations with concurrent
green threads, which IMO is the best kind of threads. Rubinius
originally planned to do concurrent green threads, but they never did.
They started off with serialized green threads, then switched to
serialized native threads and are currently in the process of
switching to concurrent native threads.)

Some JVMs started out with M:N threading (which I think is what you
mean by concurrent green threads, i.e. M green threads mapped to N
native threads, so you can get concurrency but also lightweight
threading), but as far as I know they all abandoned it due to the
overhead of managing both concurrency and lightweight thread contexts.

If there are any green-threaded JVMs out there, it's likely they're
too slow to be useful; native JIT is hard to implement atop green
threads, so green-threaded JVMs in the past were mostly interpreted.

- Charlie
 
R

Robert Klemme

2011/3/6 Charles Oliver Nutter said:
Some JVMs started out with M:N threading (which I think is what you
mean by concurrent green threads, i.e. M green threads mapped to N
native threads, so you can get concurrency but also lightweight
threading), but as far as I know they all abandoned it due to the
overhead of managing both concurrency and lightweight thread contexts.

It would also seem inefficient from the point of view that the OS does
already do all the tasks necessary for thread scheduling. So adding
another layer which duplicates these things would really only make
sense if the scheduling inside the process could benefit from
knowledge about the application's (Ruby interpreter in this case)
behavior and thus yield significantly better results than relying on
OS scheduling does. But, as you said, it's really the question
whether there is a runtime benefit and whether it's worth the added
complexity in the interpreter.
If there are any green-threaded JVMs out there, it's likely they're
too slow to be useful; native JIT is hard to implement atop green
threads, so green-threaded JVMs in the past were mostly interpreted.

Thanks for all those insights, Charles and J=F6rg!

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top