threads and extension module initialization

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pianomaestro@gmail.com

    threads and extension module initialization


    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.

  • Gabriel Genellina

    #2
    Re: threads and extension module initialization

    En Fri, 28 Mar 2008 11:51:10 -0300, <pianomaestro@g mail.comescribi ó:
    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

    Comment

    • pianomaestro@gmail.com

      #3
      Re: threads and extension module initialization

      On Mar 28, 11:14 am, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
      wrote:
      En Fri, 28 Mar 2008 11:51:10 -0300, <pianomaes...@g mail.comescribi ó:
      >
      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

      Comment

      • pianomaestro@gmail.com

        #4
        Re: threads and extension module initialization

        On Mar 28, 11:14 am, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
        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

        Comment

        • Gabriel Genellina

          #5
          Re: threads and extension module initialization

          En Fri, 28 Mar 2008 12:25:55 -0300, <pianomaestro@g mail.comescribi ó:
          On Mar 28, 11:14 am, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
          wrote:
          >En Fri, 28 Mar 2008 11:51:10 -0300, <pianomaes...@g mail.comescribi ó:
          >>
          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!=NUL L) {
          ...initialize object...
          my_object = ...;
          }

          That's a pretty standard idiom I think.

          --
          Gabriel Genellina

          Comment

          • Diez B. Roggisch

            #6
            Re: threads and extension module initialization

            pianomaestro@gm ail.com 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

            Comment

            • Gabriel Genellina

              #7
              Re: threads and extension module initialization

              En Fri, 28 Mar 2008 13:17:44 -0300, Diez B. Roggisch <deets@nospam.w eb.de>
              escribió:
              >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

              Comment

              Working...