Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting a new thread in a foreach loop

Tags:

I have a List of objects and I'd like to loop over that list and start a new thread, passing in the current object.

I've written an example of what I thought should do this, but it's not working. Specifically, it seems like the threads are getting overwritten on each iteration. This doesn't really make sense to me though because I'm making a new Thread object each time.

This is the test code I wrote

class Program {     static void Main(string[] args)     {         TestClass t = new TestClass();         t.ThreadingMethod();     } }  class TestClass {     public void ThreadingMethod()     {         var myList = new List<MyClass> { new MyClass("test1"), new MyClass("test2") };          foreach(MyClass myObj in myList)         {             Thread myThread = new Thread(() => this.MyMethod(myObj));             myThread.Start();         }     }      public void MyMethod(MyClass myObj) { Console.WriteLine(myObj.prop1); } }  class MyClass {     public string prop1 { get; set; }      public MyClass(string input) { this.prop1 = input; } } 

The output on my machine is

test2 test2 

but I expected it to be

test1 test2 

I tried changing the thread lines to

ThreadPool.QueueUserWorkItem(x => this.MyMethod(myObj)); 

but none of the threads started.

I think I just have a misunderstanding about how threads are supposed to work. Can someone point me in the right direction and tell me what I'm doing wrong?

like image 759
Kris Harper Avatar asked Feb 23 '12 18:02

Kris Harper


People also ask

Can you change a foreach loop?

Why C#'s foreach loop cannot change what it loops over. With a foreach loop we easily iterate over all elements in a collection. During each pass through the loop, the loop variable is set to an element from a collection.

Is foreach multithreaded?

ForEach loop works like a Parallel. For loop. The loop partitions the source collection and schedules the work on multiple threads based on the system environment. The more processors on the system, the faster the parallel method runs.

Is parallel foreach faster than foreach?

The execution of Parallel. Foreach is faster than normal ForEach.

How do you run a foreach loop?

How foreach loop works? The in keyword used along with foreach loop is used to iterate over the iterable-item . The in keyword selects an item from the iterable-item on each iteration and store it in the variable element . On first iteration, the first item of iterable-item is stored in element.


1 Answers

This is because you're closing over a variable in the wrong scope. The solution here is to use a temporary in your foreach loop:

    foreach(MyClass myObj in myList)     {         MyClass tmp = myObj; // Make temporary         Thread myThread = new Thread(() => this.MyMethod(tmp));         myThread.Start();     } 

For details, I recommend reading Eric Lippert's post on this exact subject: Closing over the loop variable considered harmful

like image 188
Reed Copsey Avatar answered Nov 27 '22 19:11

Reed Copsey