Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eric Lippert and Neal Gafter C# Puzzle [duplicate]

Tags:

c#

I believe that problem is caused by static field initalizator. I've spotted that new thread is started only when doInitialize is done (despite thread.Start() is called) - so I suppose that CLR blocks other threads to avoid concurrent access / double field initalization.

To sum up: Newly-created thread is not started by CLR to avoid concurrent access, but main initalization thread waits for child thread to be done what means deadlock.

Edit

@Sebastian proposed (in a comment) the link that may prove my theory: http://blogs.msdn.com/b/pfxteam/archive/2011/05/03/10159682.aspx


doInitialize is executed when the static type is constructed and then halts until the thread that sets PI terminates.

The thread that tries to set PI however cannot run until the type is initialized, which only happens once the initialization (static constructor and static initializers) are finished—which didn’t happen yet as per above.

So the program deadlocks.

See also this answer by Eric Lippert.


Thread will never finish, so thread.Join() will never return. doInitialize() is executed from static constructor. In static constructor we are trying to set the static property, but we can't access the static property unless static constructor is finished. race