Passing parameters to the Thread. What is a joint?

const int asyncCounter = 4;
 int countGlobal = Convert.ToInt32(17);
 for (int i0 = 0; i0 < countGlobal; i0 += asyncCounter)
{
 int count;
 if (i0 + asyncCounter > countGlobal)
 count = countGlobal % asyncCounter;
else
 count = asyncCounter;
 List<thread> threads = new List<thread>();
 for (int i = 1; i <= count; ++i)
{
 Thread send = new Thread(() => senderThread(i0 + i));
send.Start();
threads.Add(send);
}
 foreach (var send in threads)
{
send.Join();
}
 }</thread></thread>

The thread gets left values (e.g., 18)
I have not considered?
June 27th 19 at 15:30
2 answers
June 27th 19 at 15:32
Solution
You have not considered the time of the function call () => senderThread(i0 + i)
You may not know when the system will give time to other threads. This can happen when the cycle is finished or in the process of its implementation.
In your case it is better to use ParameterizedThreadStart and Start(object parameter)
June 27th 19 at 15:34
Still, as an option, can i copy in a separate variable, and then as you have written.

Int t0 =i0;
Int t =i;
Thread send = new Thread(() => senderThread(t0 + t));
memory for t0 and t are allocated once and will be the same as it was - cole_Rutherford commented on June 27th 19 at 15:37

Find more questions by tags C#