threads and extension module initialization

Discussion in 'Python' started by pianomaestro@gmail.com, Mar 28, 2008.

  1. Guest

    I have an extension module that gets initialized multiple
    times because I am using threads.

    How can this module access global state (not per-thread state) ?
    It needs to create a singleton.

    Simon.
     
    , Mar 28, 2008
    #1
    1. Advertising

  2. En Fri, 28 Mar 2008 11:51:10 -0300, <> escribió:

    > I have an extension module that gets initialized multiple
    > times because I am using threads.


    And do you want thread local variables?

    > How can this module access global state (not per-thread state) ?
    > It needs to create a singleton.


    C global variables are global, not per-thread.

    --
    Gabriel Genellina
     
    Gabriel Genellina, Mar 28, 2008
    #2
    1. Advertising

  3. Guest

    On Mar 28, 11:14 am, "Gabriel Genellina" <>
    wrote:
    > En Fri, 28 Mar 2008 11:51:10 -0300, <> escribió:
    >
    > > I have an extension module that gets initialized multiple
    > > times because I am using threads.

    >
    > And do you want thread local variables?


    no

    >
    > > How can this module access global state (not per-thread state) ?
    > > It needs to create a singleton.

    >
    > C global variables are global, not per-thread.


    Yes, but they get initialized once per-thread, therefore my singleton
    gets
    created multiple times.

    Simon.

    >
    > --
    > Gabriel Genellina
     
    , Mar 28, 2008
    #3
  4. Guest

    On Mar 28, 11:14 am, "Gabriel Genellina" <>
    wrote:

    > C global variables are global, not per-thread.


    aha, a static pointer gets initialized to NULL, so I
    can check if it's not NULL in the module initializer.

    Thanks for jogging my brain,

    Simon.

    >
    > --
    > Gabriel Genellina
     
    , Mar 28, 2008
    #4
  5. En Fri, 28 Mar 2008 12:25:55 -0300, <> escribió:

    > On Mar 28, 11:14 am, "Gabriel Genellina" <>
    > wrote:
    >> En Fri, 28 Mar 2008 11:51:10 -0300, <> escribió:
    >>
    >> > How can this module access global state (not per-thread state) ?
    >> > It needs to create a singleton.

    >>
    >> C global variables are global, not per-thread.

    >
    > Yes, but they get initialized once per-thread, therefore my singleton
    > gets
    > created multiple times.


    Either don't call the initialization from every thread, or guard it with
    something like:

    a_big_object my_object = NULL;
    ....

    if (my_object!=NULL) {
    ...initialize object...
    my_object = ...;
    }

    That's a pretty standard idiom I think.

    --
    Gabriel Genellina
     
    Gabriel Genellina, Mar 28, 2008
    #5
  6. schrieb:
    > I have an extension module that gets initialized multiple
    > times because I am using threads.
    >
    > How can this module access global state (not per-thread state) ?
    > It needs to create a singleton.


    The question is very unclear.

    If you are after *not* initializing your module concurrently, why don't
    you just do it *once* before the threads are started? alternatively, you
    need to govern the initialization-code with a mutex - or anything similar.

    Diez
     
    Diez B. Roggisch, Mar 28, 2008
    #6
  7. En Fri, 28 Mar 2008 13:17:44 -0300, Diez B. Roggisch <>
    escribió:
    > schrieb:


    >> I have an extension module that gets initialized multiple
    >> times because I am using threads.
    >>
    >> How can this module access global state (not per-thread state) ?
    >> It needs to create a singleton.

    >
    > The question is very unclear.
    >
    > If you are after *not* initializing your module concurrently, why don't
    > you just do it *once* before the threads are started? alternatively, you
    > need to govern the initialization-code with a mutex - or anything
    > similar.


    Ouch... The simple "if (foo!=NULL)" I posted earlier is not thread safe
    and should not be used in this situation. Do as Diez said.

    --
    Gabriel Genellina
     
    Gabriel Genellina, Mar 28, 2008
    #7
    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. Ilariu Raducan
    Replies:
    2
    Views:
    349
    Ilariu Raducan
    Jul 14, 2004
  2. Jess
    Replies:
    23
    Views:
    933
  3. Replies:
    0
    Views:
    279
  4. Replies:
    5
    Views:
    1,984
    Arne Mertz
    Mar 26, 2009
  5. Taras_96
    Replies:
    3
    Views:
    589
    Michael Tsang
    Oct 30, 2009
Loading...

Share This Page