# How to implement signal.wait(timeout)?

**URL:** <https://www.ruby-forum.com/t/how-to-implement-signal-wait-timeout/149835>\
**Category:** Ruby\
**Created:** [September 30, 2008, 12:00pm UTC](https://www.ruby-forum.com/t/how-to-implement-signal-wait-timeout/149835 "2008-09-30T12:00:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jacob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f1d935/32.png) [@jacob](https://www.ruby-forum.com/u/jacob)\
**Post date:** [September 30, 2008, 12:00pm UTC](https://www.ruby-forum.com/t/how-to-implement-signal-wait-timeout/149835/1 "2008-09-30T12:00:57Z")

</div>

Hi all

I am writing a ruby server. Something like:

class MyServer

def initialize  
@stop\_signal = MySignal.new  
end

def start  
@stop\_signal.reset  
@thread = Thread.start { run }  
end

def stop  
@stop\_signal.set  
@thread.wait  
end

def run

while true  
do\_process if there\_is\_some\_thing\_to\_process  
@stop\_signal.wait 5  
end

end

end

Is there any class in ruby that has similar functions to MySignal?  
It’s easy solution in java or .net.

I have googled, and I found a method call ‘timeout’, but after read  
the source code, I found it uses a new thread to handle timeout. I  
think It’s not good solution, because on my real system (ported  
from .net source code), there are some thread running parallel, and  
there are using alot of signal.

Thanks  
Jacob

---

<div class="post-metadata">

**Author:** ![jacob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f1d935/32.png) [@jacob](https://www.ruby-forum.com/u/jacob)\
**Post date:** [September 30, 2008, 1:41pm UTC](https://www.ruby-forum.com/t/how-to-implement-signal-wait-timeout/149835/2 "2008-09-30T13:41:31Z")

</div>

On Tue, Sep 30, 2008 at 7:59 PM, Jacob [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have googled, and I found a method call ‘timeout’, but after read  
> the source code, I found it uses a new thread to handle timeout. I  
> think It’s not good solution, because on my real system (ported  
> from .net source code), there are some thread running parallel, and  
> there are using alot of signal.

You will have to code carefully because ruby uses green threads, not  
native threads so you can get blocked on system calls.

I had the same sort of problem, in the end, we solved it with the  
terminator gem (1)

See the system timer (2) write up for a good explanation of what is  
happening.

1. [http://www.lindsaar.net/2008/9/11/terminator-timeout-without-mercy](http://www.lindsaar.net/2008/9/11/terminator-timeout-without-mercy)
2. [http://ph7spot.com/articles/system\_timer](http://ph7spot.com/articles/system_timer)

Mikel

---

<div class="post-metadata">

**Author:** ![jacob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f1d935/32.png) [@jacob](https://www.ruby-forum.com/u/jacob)\
**Post date:** [October 1, 2008, 1:45pm UTC](https://www.ruby-forum.com/t/how-to-implement-signal-wait-timeout/149835/3 "2008-10-01T13:45:06Z")

</div>

Also see the ALARM signal which allows a signal to be delivered after  
some time has elapsed.
