# Thread starvation

**URL:** <https://www.ruby-forum.com/t/thread-starvation/158050>\
**Category:** Ruby\
**Created:** [January 18, 2009, 7:06pm UTC](https://www.ruby-forum.com/t/thread-starvation/158050 "2009-01-18T19:06:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jakub\_PavlSSS\_k\_jn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@Jakub\_PavlSSS\_k\_jn](https://www.ruby-forum.com/u/Jakub_PavlSSS_k_jn)\
**Post date:** [January 18, 2009, 7:06pm UTC](https://www.ruby-forum.com/t/thread-starvation/158050/1 "2009-01-18T19:06:18Z")

</div>

Hello,

In my game freeVikings [[http://freevikings.sf.net](http://freevikings.sf.net)] I have code listed  
below.  
It uses threads and sometimes problem known as “thread starvation”  
occurs.  
Does anyone know some solution?  
(A more detailed description is under the code listing.)

* * *

def do\_loading(&block)  
Thread.abort\_on\_exception = true

load\_t = Thread.new {  
Thread.current.priority = 1  
Thread.stop

```
block.call

```

}

progressbar\_t = Thread.new {  
Thread.current.priority = 2  
load\_t.run

```
loop {
  paint_loading_screen @app_window, true
  sleep 0.3
}

```

}

## load\_t.join Thread.kill progressbar\_t end

It’s an instance method of class Game and is used in this way:  
a\_game.do\_loading {

# load something here

}

Purpose of this method is to show a simple progressbar while doing some  
long-lasting operation (e.g. loading new level).

It creates two threads: load\_t, in which given block is executed  
and progressbar\_t, which regularly calls Game#paint\_loading\_screen  
(method, which repaints loading screen with progressbar).

Thread progressbar\_t has higher priority, because I need it to be called  
often and not to e.g. wait for load\_t’s end.

Unfortunatelly from time to time Thread load\_t is somehow “suspended” -  
progressbar shows progress on and on, but nothing happens.  
I think it’s a case of so called “thread starvation” and would be very  
glad  
to know how to avoid it.

Thank you very much.

Jakub Pavlik

–  
“Configure complete, now type ‘make’ and PRAY.”

```
            (configure script of zsnes - www.zsnes.com)

```

---

<div class="post-metadata">

**Author:** ![Jakub\_PavlSSS\_k\_jn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@Jakub\_PavlSSS\_k\_jn](https://www.ruby-forum.com/u/Jakub_PavlSSS_k_jn)\
**Post date:** [January 18, 2009, 8:15pm UTC](https://www.ruby-forum.com/t/thread-starvation/158050/2 "2009-01-18T20:15:32Z")

</div>

On 18.01.2009 19:04, Jakub Pavlík jn. wrote:

> (method, which repaints loading screen with progressbar).
> 
> Thread progressbar\_t has higher priority, because I need it to be called  
> often and not to e.g. wait for load\_t’s end.
> 
> Unfortunatelly from time to time Thread load\_t is somehow “suspended” -  
> progressbar shows progress on and on, but nothing happens.  
> I think it’s a case of so called “thread starvation” and would be very glad  
> to know how to avoid it.

I guess this depends on what you do in “block”. It’s difficult to  
analyze this without having all the information…

Can you provide more detail?

Cheers

robert

---

<div class="post-metadata">

**Author:** ![Jakub\_PavlSSS\_k\_jn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@Jakub\_PavlSSS\_k\_jn](https://www.ruby-forum.com/u/Jakub_PavlSSS_k_jn)\
**Post date:** [January 19, 2009, 10:08am UTC](https://www.ruby-forum.com/t/thread-starvation/158050/3 "2009-01-19T10:08:09Z")

</div>

> > def do\_loading(&block)  
> > Thread.current.priority = 2  
> > end  
> > It creates two threads: load\_t, in which given block is executed
> 
> remember.guy do |as, often| as.you\_can - without end

Method do\_loading is used for two purposes:

1. loading of a world:  
code in the block goes through a tree of directories and in each  
reads  
a XML file
2. loading of a level:  
two XML files (level description & map data created in Tiled) +  
one ruby script (which often requires many others) are loaded from a  
given directory, many graphics are loaded and huge SDL surfaces are  
built

Thread starvation usually occurs in the second case.

Jakub

–  
“Configure complete, now type ‘make’ and PRAY.”

```
            (configure script of zsnes - www.zsnes.com)
```

---

<div class="post-metadata">

**Author:** ![Jakub\_PavlSSS\_k\_jn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@Jakub\_PavlSSS\_k\_jn](https://www.ruby-forum.com/u/Jakub_PavlSSS_k_jn)\
**Post date:** [January 19, 2009, 11:22am UTC](https://www.ruby-forum.com/t/thread-starvation/158050/4 "2009-01-19T11:22:10Z")

</div>

2009/1/19 Jakub Pavlík jn. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > > def do\_loading(&block)  
> > > Thread.current.priority = 2  
> > > end  
> > > It creates two threads: load\_t, in which given block is executed
> 
> two XML files (level description & map data created in Tiled) +  
> one ruby script (which often requires many others) are loaded from a  
> given directory, many graphics are loaded and huge SDL surfaces are built
> 
> Thread starvation usually occurs in the second case.

This is still pretty vague IMHO. Did you try to trace execution via  
set\_trace\_func? That might help.

Cheers

robert
