Consider this code:
for(int i = 0; i < 10; i ++)
{
new Thread(() => Test(i)).Start();
}
The Test(int i)
function:
public void Test(int i)
{
Console.WriteLine("=== Test " + i);
}
The actual output:
=== Test 3
=== Test 4
=== Test 4
=== Test 5
=== Test 5
=== Test 5
=== Test 9
=== Test 9
=== Test 9
=== Test 10
as you can see some numbers are missing and some other been duplicated.
Expected output:
I expect to see all numbers in random order.
Question
Should I lock any variables/methods? How can I fix this?
Should I lock any variables/methods? How can I fix this?
Your problem is with Closure and Captured Variables
Change your code as
for(int i = 0; i < 10; i ++)
{
int tmp = i;
new Thread(() => Test(tmp)).Start();
}
For more info: http://csharpindepth.com/articles/chapter5/closures.aspx or http://geekswithblogs.net/rajeevr/archive/2012/02/26/closures-and-captured-variable.aspx
There's an overload of the Thread.Start method that takes a parameter. Using that, you avoid the closure:
for(int i = 0; i < 10; i ++)
{
new Thread(o => Test((int)o)).Start(i);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With