By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,846 Members | 1,862 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,846 IT Pros & Developers. It's quick & easy.

threads and extension module initialization

P: n/a

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 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
En Fri, 28 Mar 2008 11:51:10 -0300, <pi**********@gmail.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

Mar 28 '08 #2

P: n/a
On Mar 28, 11:14 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Fri, 28 Mar 2008 11:51:10 -0300, <pianomaes...@gmail.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
Mar 28 '08 #3

P: n/a
On Mar 28, 11:14 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
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 '08 #4

P: n/a
En Fri, 28 Mar 2008 12:25:55 -0300, <pi**********@gmail.comescribió:
On Mar 28, 11:14 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
>En Fri, 28 Mar 2008 11:51:10 -0300, <pianomaes...@gmail.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!=NULL) {
...initialize object...
my_object = ...;
}

That's a pretty standard idiom I think.

--
Gabriel Genellina

Mar 28 '08 #5

P: n/a
pi**********@gmail.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
Mar 28 '08 #6

P: n/a
En Fri, 28 Mar 2008 13:17:44 -0300, Diez B. Roggisch <de***@nospam.web.de>
escribió:
pi**********@gmail.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.
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

Mar 28 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.