static constructor multithreaded?

A

Adam

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
 
J

Joanna Carter [TeamB]

"Adam" <[email protected]> a écrit dans le message de (e-mail address removed)...

| 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
 
C

Carl Daniel [VC++ MVP]

Joanna said:
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
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Carl said:
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
 

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

Top