threads and extension module initialization

P

pianomaestro

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.
 
G

Gabriel Genellina

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.
 
P

pianomaestro

And do you want thread local variables?
no


C global variables are global, not per-thread.

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

Simon.
 
P

pianomaestro

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.
 
G

Gabriel Genellina

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.
 
D

Diez B. Roggisch

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
 
G

Gabriel Genellina

(e-mail address removed) schrieb:

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top