# Threads, do\_nanosleep and battery life

**URL:** <https://www.ruby-forum.com/t/threads-do-nanosleep-and-battery-life/131812>\
**Category:** Ruby\
**Created:** [March 13, 2008, 2:28pm UTC](https://www.ruby-forum.com/t/threads-do-nanosleep-and-battery-life/131812 "2008-03-13T14:28:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mslagell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mslagell](https://www.ruby-forum.com/u/mslagell)\
**Post date:** [March 13, 2008, 2:28pm UTC](https://www.ruby-forum.com/t/threads-do-nanosleep-and-battery-life/131812/1 "2008-03-13T14:28:54Z")

</div>

I’m writing a Ruby GTK application that runs on a mobile device (Nokia  
N800).  
It’s starting to take shape now, but I have noticed that leaving it  
running for  
long periods of time is particularly bad for my battery life.

I don’t really understand Ruby threads under the hood or what  
alterations to  
them ruby-gtk2 makes (but am keen to learn!). Powertop suggests that  
Ruby is  
waking up around 100 times a second when there is an active thread, even  
during a sleep state. Presumably this is to check for thread activity.

Is there a way around this in Ruby 1.8? Will the new threading model in  
1.9  
help this?

Any pointers are apprecited,

Mark

---

<div class="post-metadata">

**Author:** ![mslagell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mslagell](https://www.ruby-forum.com/u/mslagell)\
**Post date:** [March 15, 2008, 2:14am UTC](https://www.ruby-forum.com/t/threads-do-nanosleep-and-battery-life/131812/2 "2008-03-15T02:14:45Z")

</div>

Hi,

In [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
“Threads, do\_nanosleep and battery life” on Thu, 13 Mar 2008 22:21:58  
+0900,  
Mark S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m writing a Ruby GTK application that runs on a mobile device (Nokia N800).  
> It’s starting to take shape now, but I have noticed that leaving it running for  
> long periods of time is particularly bad for my battery life.
> 
> I don’t really understand Ruby threads under the hood or what alterations to  
> them ruby-gtk2 makes (but am keen to learn!). Powertop suggests that Ruby is  
> waking up around 100 times a second when there is an active thread, even  
> during a sleep state. Presumably this is to check for thread activity.

This should be fixed in Ruby-GNOME2 trunk.

Thanks,

---

<div class="post-metadata">

**Author:** ![mslagell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mslagell](https://www.ruby-forum.com/u/mslagell)\
**Post date:** [September 23, 2008, 9:47pm UTC](https://www.ruby-forum.com/t/threads-do-nanosleep-and-battery-life/131812/3 "2008-09-23T21:47:06Z")

</div>

On Sat, Mar 15, 2008 at 4:06 AM, Kouhei S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > I’m writing a Ruby GTK application that runs on a mobile device (Nokia N800).  
> > It’s starting to take shape now, but I have noticed that leaving it running for  
> > long periods of time is particularly bad for my battery life.
> > 
> > I don’t really understand Ruby threads under the hood or what alterations to  
> > them ruby-gtk2 makes (but am keen to learn!). Powertop suggests that Ruby is  
> > waking up around 100 times a second when there is an active thread, even  
> > during a sleep state. Presumably this is to check for thread activity.
> 
> This should be fixed in Ruby-GNOME2 trunk.

Hi all,

Can you share some more details on how exactly it was addressed? I  
have a multi-threaded Ruby daemon that has nothing to do with Gtk, and  
it exhibits the same behaviour: 100 calls to do\_nanosleep per second.  
The deamon is a DRb based object cache, the whole source is these two  
files, 191 lines total:

[http://cvs.savannah.gnu.org/viewvc/\*checkout\*/samizdat/samizdat/lib/samizdat/cache.rb?revision=1.99](http://cvs.savannah.gnu.org/viewvc/*checkout*/samizdat/samizdat/lib/samizdat/cache.rb?revision=1.99)  
[http://cvs.savannah.gnu.org/viewvc/\*checkout\*/samizdat/samizdat/bin/samizdat-drb-server?revision=1.99](http://cvs.savannah.gnu.org/viewvc/*checkout*/samizdat/samizdat/bin/samizdat-drb-server?revision=1.99)

This definitely looks like a generic Ruby issue to me, but if there is  
a workaround, I don’t mind applying it on the application level.

[ sorry for waking up this really old thread, but Google tells me that  
is the only case where Ruby do\_nanosleep issue was addressed ]

---

<div class="post-metadata">

**Author:** ![mslagell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mslagell](https://www.ruby-forum.com/u/mslagell)\
**Post date:** [September 24, 2008, 11:14pm UTC](https://www.ruby-forum.com/t/threads-do-nanosleep-and-battery-life/131812/4 "2008-09-24T23:14:32Z")

</div>

> Hi all,
> 
> Can you share some more details on how exactly it was addressed? I  
> have a multi-threaded Ruby daemon that has nothing to do with Gtk, and  
> it exhibits the same behaviour: 100 calls to do\_nanosleep per second.  
> The deamon is a DRb based object cache, the whole source is these two  
> files, 191 lines total:
> 
> [http://cvs.savannah.gnu.org/viewvc/\*checkout\*/samizdat/samizdat/lib/samizdat/cache.rb?revision=1.99](http://cvs.savannah.gnu.org/viewvc/*checkout*/samizdat/samizdat/lib/samizdat/cache.rb?revision=1.99)  
> [http://cvs.savannah.gnu.org/viewvc/\*checkout\*/samizdat/samizdat/bin/samizdat-drb-server?revision=1.99](http://cvs.savannah.gnu.org/viewvc/*checkout*/samizdat/samizdat/bin/samizdat-drb-server?revision=1.99)
> 
> This definitely looks like a generic Ruby issue to me, but if there is  
> a workaround, I don’t mind applying it on the application level.
> 
> [ sorry for waking up this really old thread, but Google tells me that  
> is the only case where Ruby do\_nanosleep issue was addressed ]

[http://groups.google.com/group/never-block/browse\_thread/thread/b806a8443ffb5f45?hl=en](http://groups.google.com/group/never-block/browse_thread/thread/b806a8443ffb5f45?hl=en)  
explains a little on how Ruby does threading.  
GL!  
-=R
