static constructor multithreaded?

  • Thread starter Thread starter Adam
  • Start date Start date
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
 
"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
 
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
 
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
 
Back
Top