C# Multithreading Pass by value or reference? -


i saw piece of code online states calling thread causes non-deterministic output "0223557799" or of kind.(you point)

for (int = 0; < 10; i++)    new thread (() => console.write (i)).start(); 

this reason given this:

"the problem variable refers same memory location throughout loop’s lifetime. therefore, each thread calls console.write on variable value may change running!"

but, conventionally speaking when argument passed value - each new thread call should send i incremental order right ? if values pass reference above mentioned reason holds good. so, in c# multi-threading, values pass reference ?

i'm new c#,please understand if question naive.

there more 1 problem here. starts for() loop variable capture problem, described in blog post. tends produce "10" output, there's no guarantee happens since thread may execute while loop still being iterated.

but program suffers core threading problem, there's no guarantee whatsoever threads call console.write() in expected order. merely likely, 1 thread may race ahead of , acquire lock protects console. problem known "threading race".


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -