Thread starvation

Discussion in 'Ruby' started by Jakub Pavlík jn., Jan 18, 2009.

  1. Hello,

    In my game freeVikings [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)
    Jakub Pavlík jn., Jan 18, 2009
    #1
    1. Advertising

  2. On 18.01.2009 19:04, Jakub Pavlík jn. wrote:
    > Hello,
    >
    > In my game freeVikings [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.


    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

    --
    remember.guy do |as, often| as.you_can - without end
    Robert Klemme, Jan 18, 2009
    #2
    1. Advertising

  3. > On 18.01.2009 19:04, Jakub Pavlík jn. wrote:
    > >Hello,
    > >
    > >In my game freeVikings [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.

    >
    > 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
    >
    > --
    > 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)
    Jakub Pavlík jn., Jan 19, 2009
    #3
  4. 2009/1/19 Jakub Pavl=EDk jn. <>:
    >> On 18.01.2009 19:04, Jakub Pavl=EDk jn. wrote:
    >> >Hello,
    >> >
    >> >In my game freeVikings [http://freevikings.sf.net] I have code listed
    >> >below.
    >> >It uses threads and sometimes problem known as "thread starvation" occu=

    rs.
    >> >Does anyone know some solution?
    >> >(A more detailed description is under the code listing.)
    >> >
    >> >----------------------------------
    >> >def do_loading(&block)
    >> > Thread.abort_on_exception =3D true
    >> >
    >> > load_t =3D Thread.new {
    >> > Thread.current.priority =3D 1
    >> > Thread.stop
    >> >
    >> > block.call
    >> > }
    >> >
    >> > progressbar_t =3D Thread.new {
    >> > Thread.current.priority =3D 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 calle=

    d
    >> >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?

    >
    > 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 bui=

    lt
    >
    > 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


    --=20
    remember.guy do |as, often| as.you_can - without end
    Robert Klemme, Jan 19, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles A. Lackman

    Terminating a thread from the main thread

    Charles A. Lackman, Dec 9, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    1,539
    Herfried K. Wagner [MVP]
    Dec 9, 2004
  2. pawo
    Replies:
    0
    Views:
    485
  3. Stephen Miller
    Replies:
    3
    Views:
    3,963
    Stephen Miller
    Jul 2, 2004
  4. Humphreys, Brett

    Thread Starvation

    Humphreys, Brett, Sep 14, 2004, in forum: Python
    Replies:
    0
    Views:
    525
    Humphreys, Brett
    Sep 14, 2004
  5. John Nagle

    CPython thread starvation

    John Nagle, Apr 27, 2012, in forum: Python
    Replies:
    11
    Views:
    554
    Roy Smith
    Apr 29, 2012
Loading...

Share This Page