Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > threads and extension module initialization

Reply
Thread Tools

threads and extension module initialization

 
 
pianomaestro@gmail.com
Guest
Posts: n/a
 
      03-28-2008

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.

 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      03-28-2008
En Fri, 28 Mar 2008 11:51:10 -0300, <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
pianomaestro@gmail.com
Guest
Posts: n/a
 
      03-28-2008
On Mar 28, 11:14 am, "Gabriel Genellina" <(E-Mail Removed)>
wrote:
> En Fri, 28 Mar 2008 11:51:10 -0300, <(E-Mail Removed)> 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


 
Reply With Quote
 
pianomaestro@gmail.com
Guest
Posts: n/a
 
      03-28-2008
On Mar 28, 11:14 am, "Gabriel Genellina" <(E-Mail Removed)>
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


 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      03-28-2008
En Fri, 28 Mar 2008 12:25:55 -0300, <(E-Mail Removed)> escribiˇ:

> On Mar 28, 11:14 am, "Gabriel Genellina" <(E-Mail Removed)>
> wrote:
>> En Fri, 28 Mar 2008 11:51:10 -0300, <(E-Mail Removed)> 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

 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      03-28-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      03-28-2008
En Fri, 28 Mar 2008 13:17:44 -0300, Diez B. Roggisch <(E-Mail Removed)>
escribiˇ:
> (E-Mail Removed) 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
value & default initialization, and copy initialization Taras_96 C++ 3 10-30-2009 10:51 AM
copy initialization and direct initialization from C++ Primer pauldepstein@att.net C++ 5 03-26-2009 06:32 PM
extension module initialization called twice pianomaestro@gmail.com Python 0 03-27-2008 09:35 PM
what's the difference between value-initialization and default-initialization? Jess C++ 23 05-04-2007 03:03 AM
Default Initialization Vs. Value Initialization JKop C++ 10 09-22-2004 07:26 PM



Advertisments