c# - If a volatile reference has changed between a thread loading the reference and calling a function on it, can the old object be garbage collected? -


i have 2 threads executing code below:

static volatile foo;  void update() {     newfoo = new something();     foo = newfoo; }  void invoke() {     foo.bar(); } 

thread executes update , thread b executes invoke. 2 threads have timing such invoke loads address of foo, update overwrites foo, , garbage collection happens before bar called into.

is possible garbage collection may collect old object referenced foo, causing bar invoked on memory has been collected?

note question out of curiosity. i'm open better title.

the garbage collector pause state of running threads long enough resolve race conditions surrounding memory accesses made thereby. whether static variable foo volatile or not, garbage collector know identities of object call bar might invoked upon, , ensure such object object continue exist long there execution path via normal or "hidden" fields thereof possibly accessed, via keepalive call might performed on it, or via might reference-compared reference.

it possible system may in circumstances call finalize on object while observable references exist it, system maintains absolute invariant gc knows references used execution path in ways described above; objects guaranteed exist long such references exist.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -