469,909 Members | 1,814 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,909 developers. It's quick & easy.

static constructor multithreaded?

What happens if one thread is executing a static constructor and
another thread starts. Does the second thread block until the first is
done in the static constructor? I want to make sure all my globals are
initialized and the second thread does not throw an exception.

Thanks in advance.

Adam Smith

Oct 8 '06 #1
3 4162
"Adam" <ad*********@hotmail.coma écrit dans le message de news:
11*********************@e3g2000cwe.googlegroups.co m...

| What happens if one thread is executing a static constructor and
| another thread starts. Does the second thread block until the first is
| done in the static constructor? I want to make sure all my globals are
| initialized and the second thread does not throw an exception.

Static constructors are only ever called once, and static methods are
implicitly threadsafe, so you shouldn't have to take any special
precautions.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Oct 8 '06 #2
Joanna Carter [TeamB] wrote:
"Adam" <ad*********@hotmail.coma écrit dans le message de news:
11*********************@e3g2000cwe.googlegroups.co m...
>What happens if one thread is executing a static constructor and
another thread starts. Does the second thread block until the first
is done in the static constructor? I want to make sure all my
globals are initialized and the second thread does not throw an
exception.

Static constructors are only ever called once, and static methods are
implicitly threadsafe, so you shouldn't have to take any special
precautions.
Static constructors are only called once, but static methods are most
certainly NOT implicitly threadsafe! If a static method modifies data (e.g.
static fields), then it needs the same thread safety considerations that a
non-static method would need. Particularly, synchronization around access
to the shared state.

-cd
Oct 8 '06 #3
Carl Daniel [VC++ MVP] wrote:
Joanna Carter [TeamB] wrote:
>"Adam" <ad*********@hotmail.coma écrit dans le message de news:
11*********************@e3g2000cwe.googlegroups.co m...
>>What happens if one thread is executing a static constructor and
another thread starts. Does the second thread block until the first
is done in the static constructor? I want to make sure all my
globals are initialized and the second thread does not throw an
exception.
Static constructors are only ever called once, and static methods are
implicitly threadsafe, so you shouldn't have to take any special
precautions.

Static constructors are only called once, but static methods are most
certainly NOT implicitly threadsafe! If a static method modifies data (e.g.
static fields), then it needs the same thread safety considerations that a
non-static method would need. Particularly, synchronization around access
to the shared state.
Should we guess that "...and static methods are..." should
have been "...and static constructors are...", because that
makes most sense in the context.

Arne
Oct 8 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Kevin C | last post: by
1 post views Thread by Ray Ackley | last post: by
2 posts views Thread by superseed | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.