Threads and sharing global variable

Discussion in 'Perl Misc' started by anguyen@redhat.com, Sep 3, 2008.

  1. Guest

    Hi,
    I am having a global array @gDone, and after spawning a number
    threads, each would set the corresponding element to TRUE to signify
    it has done its work so the parent thread could stop waiting.
    It seems like the update of @gDone is not seen in the parent thread.
    The print of $gDone[$tid] is always either FALSE or undef, depending
    on how I set it in the parent thread.
    Can someone help providing an explanation for this?
    Anh-
     
    , Sep 3, 2008
    #1
    1. Advertising

  2. On Wed, 03 Sep 2008 09:44:02 -0700, anguyen wrote:

    > Hi,
    > I am having a global array @gDone, and after spawning a number threads,
    > each would set the corresponding element to TRUE to signify it has done
    > its work so the parent thread could stop waiting. It seems like the
    > update of @gDone is not seen in the parent thread. The print of
    > $gDone[$tid] is always either FALSE or undef, depending on how I set it
    > in the parent thread. Can someone help providing an explanation for
    > this? Anh-


    You haven't posted any code, did you use thread::shared?

    Leon Timmermans
     
    Leon Timmermans, Sep 3, 2008
    #2
    1. Advertising

  3. zentara Guest

    On Wed, 3 Sep 2008 09:44:02 -0700 (PDT), wrote:

    >Hi,
    >I am having a global array @gDone, and after spawning a number
    >threads, each would set the corresponding element to TRUE to signify
    >it has done its work so the parent thread could stop waiting.
    >It seems like the update of @gDone is not seen in the parent thread.
    >The print of $gDone[$tid] is always either FALSE or undef, depending
    >on how I set it in the parent thread.
    >Can someone help providing an explanation for this?
    >Anh-


    A couple of possible explanations:
    1. You set the values of the array before you declared it shared.
    2. The parent thread will not automagically see the changes in the array
    due to a thread action, you must active read it in the main thread to
    update it. This is a problem in Gui's and threads, where you need a
    timer run in the main thread to constantly re-read the array at a fast
    interval.

    Otherwise, show a minimal code example that runs and demonstrates the
    problem.

    zentara


    --
    I'm not really a human, but I play one on earth.
    http://zentara.net/Remember_How_Lucky_You_Are.html
     
    zentara, Sep 4, 2008
    #3
    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. Dennis Gavrilov
    Replies:
    1
    Views:
    1,471
    Dennis Gavrilov
    Jul 24, 2003
  2. Ram P. Dash

    Sharing DLL between threads

    Ram P. Dash, May 27, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    626
    Ziga Jakhel
    May 31, 2005
  3. Ron Mexico
    Replies:
    5
    Views:
    418
    Patrice
    Nov 2, 2005
  4. Replies:
    3
    Views:
    683
    jan V
    Sep 14, 2005
  5. J Rice
    Replies:
    5
    Views:
    426
    J Rice
    Mar 18, 2006
Loading...

Share This Page