This is because
count
is thread-static, one task is using this value, and another one uses hard-coded
immediate constant 20 (bad by itself). A thread-static field becomes not static, the attribute
ThreadStaticAttribute
makes it behave as two or more different static fields per each thread, which are, importantly,
are initialized separately in each thread. Its initial value becomes 0, so the loop using it will undergo zero number of iterations. To see it, if would be enough to output
count
before each loop.
Please see:
https://msdn.microsoft.com/en-us/library/system.threadstaticattribute%28v=vs.110%29.aspx[
^].
Note that having such field would be pointless, because this is constant, so its value can be used in two thread. To make both tasks' loops work, you would need to declare
const count = 20;
and remove hard-coded 20 from other places.
Note that your experiment is not 100% correct, because
tasks are not exactly threads.
—SA