1

I call with threads recursive function. But not work, because all threads have same parameters: problem is i. All i=st. Later all recursive function not working ok.

threads = new Thread[st];
for (int i = 1; i <= st; i++)
 {
 Thread t1 = new Thread(() =>
 {
 rek_md5(crke, i, new char[i], 0, md5Hash);
 });
 t1.Name = i.ToString();
 threads[i-1] = t1;
 t1.Start();
 }

How can I fix, all threads have different parameters.

1 Answers1

0

This is because the lambda makes a closure over i, such that the code inside the lambda always sees the value of i that ended the for loop, because that's the value of i by the time the lambda runs. Here's some more info:

http://blogs.msdn.com/b/ericlippert/archive/2009/11/12/closing-over-the-loop-variable-considered-harmful.aspx

adv12
  • 8,443
  • 2
  • 24
  • 48