Starting a thread with / without delegate() Starting a thread with / without delegate() multithreading multithreading

Starting a thread with / without delegate()


With the delegate, you are capturing i.

The difference is that with new ThreadStart((new A(i)).Do)), you are creating a new instance of A in the for loop with i as a parameter. That means that at that point, the value of i is taken and send to the constructor. The delegate you are sending is thus not of the creation of A, but you are actually sending the a delegate of the Do method of the instance of A to the constructor.

However, with delegate() { (new A(i)).Do(); }) (both of them), you are sending a reference of i to the thread.

The thread then takes some time to start and meanwhile, the for loop goes on. By the time i is used in the delegate (i.e. the thread has started), the for loop has moved on to 3 and that's what you see. The same goes for the second and third thread. The three threads are started but wait for the starting thread to complete some work. Then the created threads kick in (thread 1, 2 and 3) and they do their work. The Windows goes back to the thread with the for loop and goes on to start thread 4 and 5.

Some reading material:


To answer your first point, delegate() { SomeFunc();} creates a function that calls SomeFunc(), whereas not using delegate() simply uses the SomeFunc function directly as the ThreadStart method.

In your second question, you're running into the implementation details of C# anonymous functions. All three references to i refer to the same i, which is incremented three times. You've got a race condition between the three functions that mean i can be incremented several times before the started threads are run.


'When is the constructor for object A called?' helps answer the question.

new ThreadStart((new A(i)).Do))

When this line of code is executed - the constructor is called and a reference to the .Do function on the newly created object A is kept by the ThreadStart delegate.

In Line 2 and 3 you are using an anonymous delegate (introduced in C# 2.0).

delegate() { (new A(i)).Do(); })

The contents of the anonymous delegate is not executed until the delegate is invoked; in this case by the thread when it is assigned a time slice to do so.

The variable i is only declared once at the start of the for loop, and the delegate contents has a reference to it (delegates will do that) - when the code is executed it must grab the value of i at the time of execution.

This explains the value 10. i has a value of 10 when the loop has finished executing. If one of the threads executes after the loop has finished, it will output a 10.

To avoid the multiple numbers issue you could create a local copy of the loop variable. The delegate will keep a reference to its own version of icopy;

for (int i = 0; i < 10; i++){     int icopy = i;     (new Thread(new ThreadStart(delegate() { (new A(icopy)).Do(); }))).Start();}