# When threads block

**URL:** <https://rubytalk.org/t/when-threads-block/8228>\
**Category:** ruby-talk\
**Created:** [24 September 2003 01:16 UTC](https://rubytalk.org/t/when-threads-block/8228 "2003-09-24T01:16:53Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hans\_Fugal](https://avatars.discourse-cdn.com/v4/letter/h/e47774/32.png) [@Hans\_Fugal](https://rubytalk.org/u/Hans_Fugal)\
**Post date:** [24 September 2003 01:16 UTC](https://rubytalk.org/t/when-threads-block/8228/1 "2003-09-24T01:16:53Z")

</div>

It’s difficult to do any serious multi-threaded network programming when  
the whole process blocks on system calls like read, write, and select. Is  
this on a TODO list somewhere, or should I just accept it as a fact of  
life? (and that may mean using a different language.)

---

<div class="post-metadata">

**Author:** ![Nobuyoshi\_Nakada](https://avatars.discourse-cdn.com/v4/letter/n/edb3f5/32.png) [@Nobuyoshi\_Nakada](https://rubytalk.org/u/Nobuyoshi_Nakada)\
**Post date:** [24 September 2003 06:24 UTC](https://rubytalk.org/t/when-threads-block/8228/2 "2003-09-24T06:24:03Z")

</div>

Hi,

> **···**
>
> At Wed, 24 Sep 2003 10:16:53 +0900, Hans Fugal wrote:
> 
> > It’s difficult to do any serious multi-threaded network programming when  
> > the whole process blocks on system calls like read, write, and select. Is  
> > this on a TODO list somewhere, or should I just accept it as a fact of  
> > life? (and that may mean using a different language.)
> 
> What’s your platform?
> 
> If you use Windows, it’s a known problem but hard to fix with  
> current thread implementation.
> 
> –  
> Nobu Nakada

---

<div class="post-metadata">

**Author:** ![Hans\_Fugal](https://avatars.discourse-cdn.com/v4/letter/h/e47774/32.png) [@Hans\_Fugal](https://rubytalk.org/u/Hans_Fugal)\
**Post date:** [24 September 2003 13:38 UTC](https://rubytalk.org/t/when-threads-block/8228/3 "2003-09-24T13:38:58Z")

</div>

Ok, I jumped the gun just a little. On linux /most/ things don’t block.  
But /some/ things do. e.g.

thread = Thread.new do  
fifo = File.open(“fifo”,“r”)  
l = fifo.readline  
puts “#{Time.new} #{l}”  
end

puts “#{Time.new} bar”  
thread.join

Will block the entire process until the fifo is open. The following  
variation does not block the entire process, though:

fifo = File.open(“fifo”,“r”)  
thread = Thread.new do  
l = fifo.readline  
puts “#{Time.new} #{l}”  
end

puts “#{Time.new} bar”  
thread.join

I say “most” because that’s what the wise ones on #ruby-lang said, and  
indeed were surprised to see I had a counter example.

Is there a list of these sorts of things? I need to know what to expect if  
I’m going to be able to use ruby for this (soft) real-time application.

> **···**
>
> On Tue, 23 Sep 2003 18:45:40 -0600, Hans Fugal wrote:
> 
> > It’s difficult to do any serious multi-threaded network programming when  
> > the whole process blocks on system calls like read, write, and select. Is  
> > this on a TODO list somewhere, or should I just accept it as a fact of  
> > life? (and that may mean using a different language.)

---

<div class="post-metadata">

**Author:** ![Thomas\_Sondergaard](https://avatars.discourse-cdn.com/v4/letter/t/ecae2f/32.png) [@Thomas\_Sondergaard](https://rubytalk.org/u/Thomas_Sondergaard)\
**Post date:** [24 September 2003 09:38 UTC](https://rubytalk.org/t/when-threads-block/8228/4 "2003-09-24T09:38:27Z")

</div>

> If you use Windows, it’s a known problem but hard to fix with  
> current thread implementation.

When will we get a new thread implementation, then? 🙂

Seriously, could you outline how these things are implemented on other  
platforms to avoid blocking the entire process? Why can’t this trick be used  
on the windows platform? I’d like to help if I can in any way.

Another question - is true thread safety on the TODO list for ruby? Oh and  
this begs the third question: Is there a release plan for ruby? I would love  
to know what kind of goodies I can expect for future releases?

Cheers,

Thomas

---

<div class="post-metadata">

**Author:** ![Nobuyoshi\_Nakada](https://avatars.discourse-cdn.com/v4/letter/n/edb3f5/32.png) [@Nobuyoshi\_Nakada](https://rubytalk.org/u/Nobuyoshi_Nakada)\
**Post date:** [24 September 2003 23:53 UTC](https://rubytalk.org/t/when-threads-block/8228/5 "2003-09-24T23:53:28Z")

</div>

Hi,

> Ok, I jumped the gun just a little. On linux /most/ things don’t block.  
> But /some/ things do. e.g.
> 
> thread = Thread.new do  
> fifo = File.open(“fifo”,“r”)  
> l = fifo.readline  
> puts “#{Time.new} #{l}”  
> end
> 
> puts “#{Time.new} bar”  
> thread.join

You can use IO::NONBLOCK with IO::RDONLY instead of “r”.

> Is there a list of these sorts of things? I need to know what to expect if  
> I’m going to be able to use ruby for this (soft) real-time application.

File#flock, and some Socket methods do. Use resolv-replace.rb  
for the latter.

> **···**
>
> At Wed, 24 Sep 2003 22:38:58 +0900, Hans Fugal wrote:
> 
> –  
> Nobu Nakada

---

<div class="post-metadata">

**Author:** ![Laurent\_Julliard4](https://avatars.discourse-cdn.com/v4/letter/l/ebca7d/32.png) [@Laurent\_Julliard4](https://rubytalk.org/u/Laurent_Julliard4)\
**Post date:** [24 September 2003 16:39 UTC](https://rubytalk.org/t/when-threads-block/8228/6 "2003-09-24T16:39:20Z")

</div>

> > If you use Windows, it’s a known problem but hard to fix with  
> > current thread implementation.
> 
> When will we get a new thread implementation, then? 🙂
> 
> Seriously, could you outline how these things are implemented on other  
> platforms to avoid blocking the entire process? Why can’t this trick be used  
> on the windows platform? I’d like to help if I can in any way.

The problem should be related to windows’ select().  
On \*nix IO stuff is transparently put in a select, and ruby takes care  
of giving a little time to every thread when select gives them output.  
On windows this won’t work cause select() just accepts socket  
descriptor. I wonder if these works in python.  
And is WaitForMultipleObjects() useful someway ?

> Another question - is true thread safety on the TODO list for ruby? Oh and  
> this begs the third question: Is there a release plan for ruby? I would love  
> to know what kind of goodies I can expect for future releases?

wait for Rite (ruby 2.0)

> **···**
>
> il Wed, 24 Sep 2003 11:26:47 +0200, “Thomas Sondergaard” [thomas@FirstNameGoesHereSondergaard.com](mailto:thomas@FirstNameGoesHereSondergaard.com) ha scritto::

---

<div class="post-metadata">

**Author:** ![Nathaniel\_Talbott](https://avatars.discourse-cdn.com/v4/letter/n/ec9cab/32.png) [@Nathaniel\_Talbott](https://rubytalk.org/u/Nathaniel_Talbott)\
**Post date:** [25 September 2003 00:24 UTC](https://rubytalk.org/t/when-threads-block/8228/7 "2003-09-25T00:24:45Z")

</div>

Is this intentional?

irb(main):001:0\> require ‘socket’  
=\> true  
irb(main):002:0\> TCPServer::new(55)  
=\> #TCPServer:0x2b9ef50  
irb(main):003:0\> require ‘resolv-replace’  
=\> true  
irb(main):004:0\> TCPServer::new(55)  
ArgumentError: wrong number of arguments(1 for 2)  
from (irb):4:in `new’  
from (irb):4

Because of this, using DRb with resolv-replace doesn’t work very well. Any  
idea as to what is wrong?

Thanks,

Nathaniel

\<:((\>\<

> **···**
>
> [nobu.nokada@softhome.net](mailto:nobu.nokada@softhome.net) [[mailto:nobu.nokada@softhome.net](mailto:nobu.nokada@softhome.net)] wrote:
> 
> > Hans Fugal wrote:
> > 
> > > Is there a list of these sorts of things? I need to know what to  
> > > expect if I’m going to be able to use ruby for this (soft)  
> > > real-time application.
> > 
> > File#flock, and some Socket methods do. Use  
> > resolv-replace.rb for the latter.

---

<div class="post-metadata">

**Author:** ![Lothar\_Scholz2](https://avatars.discourse-cdn.com/v4/letter/l/f14d63/32.png) [@Lothar\_Scholz2](https://rubytalk.org/u/Lothar_Scholz2)\
**Post date:** [25 September 2003 00:15 UTC](https://rubytalk.org/t/when-threads-block/8228/8 "2003-09-25T00:15:50Z")

</div>

Hello gabriele,

Wednesday, September 24, 2003, 5:39:20 PM, you wrote:

> The problem should be related to windows' select().  
> On \*nix IO stuff is transparently put in a select, and ruby takes care  
> of giving a little time to every thread when select gives them output.  
> On windows this won't work cause select() just accepts socket  
> descriptor. I wonder if these works in python.  
> And is WaitForMultipleObjects() useful someway ?

It's a joke right ?

Every serious windows programming books warns you to not use 'select'  
on a window plattform. Using WASSelect is the normal way. It is much  
better then the UNIX API and integrates perfect into the windows  
environment. But a new implementation should not use them either.  
The best is to use asynchron system calls that calls back when data  
is available, it is much faster then the LINUX ip stack implementation.

> **···**
>
> --  
> Best regards,  
> Lothar [mailto:mailinglists@scriptolutions](mailto:mailinglists@scriptolutions).com

---

<div class="post-metadata">

**Author:** ![Nobuyoshi\_Nakada](https://avatars.discourse-cdn.com/v4/letter/n/edb3f5/32.png) [@Nobuyoshi\_Nakada](https://rubytalk.org/u/Nobuyoshi_Nakada)\
**Post date:** [25 September 2003 01:50 UTC](https://rubytalk.org/t/when-threads-block/8228/9 "2003-09-25T01:50:59Z")

</div>

Hi,

> Is this intentional?
> 
> irb(main):001:0\> require ‘socket’  
> =\> true  
> irb(main):002:0\> TCPServer::new(55)  
> =\> #TCPServer:0x2b9ef50  
> irb(main):003:0\> require ‘resolv-replace’  
> =\> true  
> irb(main):004:0\> TCPServer::new(55)  
> ArgumentError: wrong number of arguments(1 for 2)  
> from (irb):4:in `new’  
> from (irb):4

No, resolv-replace.rb seems not to be 1.8 compliant.

> Because of this, using DRb with resolv-replace doesn’t work very well. Any  
> idea as to what is wrong?

Does this work?

Index: lib/resolv-replace.rb

> **···**
>
> # At Thu, 25 Sep 2003 09:24:45 +0900, Nathaniel Talbott wrote:
> 
> RCS file: /cvs/ruby/src/ruby/lib/resolv-replace.rb,v  
> retrieving revision 1.1  
> diff -u -2 -p -r1.1 resolv-replace.rb  
> — lib/resolv-replace.rb 30 May 2001 09:10:26 -0000 1.1  
> +++ lib/resolv-replace.rb 25 Sep 2003 01:49:23 -0000  
> @@ -1,2 +1,3 @@  
> +require ‘socket’  
> require ‘resolv’
> 
> @@ -4,5 +5,5 @@ class BasicSocket  
> alias original\_resolv\_send send  
> def send(mesg, flags, \*rest)
> 
> - rest[0] = Resolv.getaddress(rest[0]).to\_s if 0 \< rest.length
> 
> - rest[0] = Resolv.getaddress(rest[0]).to\_s unless rest.empty?  
> original\_resolv\_send(mesg, flags, \*rest)  
> end  
> @@ -16,17 +17,18 @@ class \<\< IPSocket  
> end
> 
> -class \<\< TCPSocket
> 
> - alias original\_resolv\_new new
> - def new(host, service)
> - original\_resolv\_new(Resolv.getaddress(host).to\_s, service)
> - end
> - 
> - alias original\_resolv\_open open
> - def open(host, service)
> - original\_resolv\_open(Resolv.getaddress(host).to\_s, service)  
> +class TCPSocket
> 
> - alias original\_resolv\_initialize initialize
> - def initialize(host, serv, \*rest)
> - rest[0] = Resolv.getaddress(rest[0]).to\_s unless rest.empty?
> - original\_resolv\_initialize(Resolv.getaddress(host).to\_s, serv, \*rest)  
> end  
> end
> 
> class UDPSocket
> 
> - alias original\_resolv\_bind bind
> - def bind(host, port)
> - original\_resolv\_bind(Resolv.getaddress(host).to\_s, port)
> - end
> - alias original\_resolv\_connect connect  
> def connect(host, port)  
> @@ -36,6 +38,13 @@ class UDPSocket  
> alias original\_resolv\_send send  
> def send(mesg, flags, \*rest)
> 
> - rest[0] = Resolv.getaddress(rest[0]).to\_s if 0 \< rest.length
> 
> - rest[0] = Resolv.getaddress(rest[0]).to\_s unless rest.empty?  
> original\_resolv\_send(mesg, flags, \*rest)  
> end  
> end
> - 
> 
> +class SOCKSSocket
> 
> - alias original\_resolv\_initialize initialize
> - def initialize(host, serv)
> - original\_resolv\_initialize(Resolv.getaddress(host).to\_s, port)
> - end  
> +end if defined? SOCKSSocket
> 
> –  
> Nobu Nakada

---

<div class="post-metadata">

**Author:** ![Nathaniel\_Talbott](https://avatars.discourse-cdn.com/v4/letter/n/ec9cab/32.png) [@Nathaniel\_Talbott](https://rubytalk.org/u/Nathaniel_Talbott)\
**Post date:** [25 September 2003 15:36 UTC](https://rubytalk.org/t/when-threads-block/8228/10 "2003-09-25T15:36:14Z")

</div>

It works great for me. Thanks Nobu!

Nathaniel

\<:((\>\<

> **···**
>
> [nobu.nokada@softhome.net](mailto:nobu.nokada@softhome.net) [[mailto:nobu.nokada@softhome.net](mailto:nobu.nokada@softhome.net)] wrote:
> 
> > resolv-replace.rb seems not to be 1.8 compliant.
> > 
> > Does this work?

---

<div class="post-metadata">

**Author:** ![Laurent\_Julliard4](https://avatars.discourse-cdn.com/v4/letter/l/ebca7d/32.png) [@Laurent\_Julliard4](https://rubytalk.org/u/Laurent_Julliard4)\
**Post date:** [25 September 2003 20:43 UTC](https://rubytalk.org/t/when-threads-block/8228/11 "2003-09-25T20:43:03Z")

</div>

> > And is WaitForMultipleObjects() useful someway ?
> 
> It’s a joke right ?
> 
> Every serious windows programming books warns you to not use ‘select’  
> on a window plattform.

That’s sure. I never ever read a windows programming book, this may  
matter 🙂  
Actually, I just wrote some win code for an OS course and at most used  
3 syscall …

> Using WASSelect is the normal way.  
> Is’nt WSASelect even stuck to FD\_SET==array of sockets?

> It is much  
> better then the UNIX API and integrates perfect into the windows  
> environment.

Sure it does, but well, ruby is mostly developed on unix 😕

anyway, I was simply referring to Message-Id:  
[200309240558.h8O5wE2s021288@sharui.nakada.kanuma.tochigi.jp](mailto:200309240558.h8O5wE2s021288@sharui.nakada.kanuma.tochigi.jp)

> **···**
>
> il Thu, 25 Sep 2003 09:15:50 +0900, Lothar Scholz [mailinglists@scriptolutions.com](mailto:mailinglists@scriptolutions.com) ha scritto::

---

<div class="post-metadata">

**Author:** ![Tanaka\_Akira1](https://avatars.discourse-cdn.com/v4/letter/t/a88e57/32.png) [@Tanaka\_Akira1](https://rubytalk.org/u/Tanaka_Akira1)\
**Post date:** [26 September 2003 05:55 UTC](https://rubytalk.org/t/when-threads-block/8228/12 "2003-09-26T05:55:36Z")

</div>

In article [200309250150.h8P1ow2s008367@sharui.nakada.kanuma.tochigi.jp](mailto:200309250150.h8P1ow2s008367@sharui.nakada.kanuma.tochigi.jp),  
[nobu.nokada@softhome.net](mailto:nobu.nokada@softhome.net) writes:

> No, resolv-replace.rb seems not to be 1.8 compliant.

Please commit it.

> **···**
>
> –  
> Tanaka Akira

---

<div class="post-metadata">

**Author:** ![Lothar\_Scholz2](https://avatars.discourse-cdn.com/v4/letter/l/f14d63/32.png) [@Lothar\_Scholz2](https://rubytalk.org/u/Lothar_Scholz2)\
**Post date:** [26 September 2003 13:25 UTC](https://rubytalk.org/t/when-threads-block/8228/13 "2003-09-26T13:25:31Z")

</div>

Hello gabriele,

> > Using WASSelect is the normal way.
> 
> Is'nt WSASelect even stuck to FD\_SET==array of sockets?

No. Its working with GUI messages.

> > It is much  
> > better then the UNIX API and integrates perfect into the windows  
> > environment.

> Sure it does, but well, ruby is mostly developed on unix 😕

But it's a "very high level language" and it fallsback to extremely  
low level plattform dependent network code. That's a design decision i don't  
understand. It's okay to wrap this, but higher modules like the HTTP,  
FTP protocol shouldn't be based on a 'select' like model.

> **···**
>
> --  
> Best regards,  
> Lothar [mailto:mailinglists@scriptolutions](mailto:mailinglists@scriptolutions).com
