0

Hope someone can spot what is going on here, I've spent hours looking and I think it is impossible.

I have 2 for loops and this passes the counters to a thread for more processing. when I use more than one thread or do not add a wait in the for loop the threads are passed the same counters.

my code

public static List<Thread> TPool = new List<Thread>();
for (int i = 1; i <= 10; i++) {
  for (int m = 2; m <= 10; m++) {
    waitingThreadPooling(new ThreadStart(() => run_test(i, m)), 3);
    //wait(10);
  }
}
private static void run_test(int the_i, int the_m)
{
  string Test = "i:" + the_i + ", m:" + the_m;
  // more code
  Console.WriteLine(Test);
}
public static void waitingThreadPooling(ThreadStart start, int MaxThreads = 50)
{
  Pool_Threads_Remove_Dead(ref TPool);
  while (true)
  {
    if (TPool.Count < MaxThreads)
    {
      Thread _thread = new Thread(start);
      _thread.Start();
      TPool.Add(_thread);
      break;
    }
    else
    {
       Pool_Threads_Remove_Dead(ref TPool);
    }
    Wait(10);
  }
}

this outputs

i:1, m:4  <<<
i:1, m:4  <<< whats going on here???
i:1, m:3
i:1, m:5
i:1, m:6
i:1, m:8
i:1, m:7
Ondrej Tucny
  • 27,626
  • 6
  • 70
  • 90
Jonathan Morrall
  • 43
  • 1
  • 2
  • 5

0 Answers0