This is a fun example that illustrates a few topics I'm discussing at PDC in a couple weeks.
What, if anything, can cause Thread 2's assert below to fire?
class Foo {
static Foo lastFoo;
string state;
bool initialized;
public Foo() {
state = "Developers, Developers, Developers!";
initialized = true;
}
}
// Thread #1: // Thread #2:
lastFoo = new Foo(); Foo f = lastFoo;
Debug.Assert(f.initialized == true &&
f.state != null);
For purposes of illustration, imagine that lastFoo has already been initialized to some Foo prior to threads #1 and #2 executing.