D
Dancefire
Hi,
I tried the code below:
for(int i = 0; i < 10; ++i)
{
int a = i;
Thread task = new Thread(delegate()
{
Console.WriteLine(string.Format("Thread start: a = {0}, i = {1}",
a, i));
});
task.Start();
}
And the output is
Thread start: a = 0, i = 2
Thread start: a = 2, i = 2
Thread start: a = 1, i = 2
Thread start: a = 3, i = 3
Thread start: a = 4, i = 5
Thread start: a = 5, i = 6
Thread start: a = 6, i = 7
Thread start: a = 7, i = 7
Thread start: a = 8, i = 9
Thread start: a = 9, i = 10
It looks like a race condition problem.
What is the problem? why i got such result? How can I make it correct
without pass parameter, such as not using ParameterizedThreadStart in
this case?
Is there any guide to use anonymous methods under multi-thread
environment?
I tried the code below:
for(int i = 0; i < 10; ++i)
{
int a = i;
Thread task = new Thread(delegate()
{
Console.WriteLine(string.Format("Thread start: a = {0}, i = {1}",
a, i));
});
task.Start();
}
And the output is
Thread start: a = 0, i = 2
Thread start: a = 2, i = 2
Thread start: a = 1, i = 2
Thread start: a = 3, i = 3
Thread start: a = 4, i = 5
Thread start: a = 5, i = 6
Thread start: a = 6, i = 7
Thread start: a = 7, i = 7
Thread start: a = 8, i = 9
Thread start: a = 9, i = 10
It looks like a race condition problem.
What is the problem? why i got such result? How can I make it correct
without pass parameter, such as not using ParameterizedThreadStart in
this case?
Is there any guide to use anonymous methods under multi-thread
environment?