4

I have a task that essentially loops through a collection and does an operation on them in pairs (for int i = 0; i < limit; i+=2 etc.) And so, most suggestions I see on threading loops use some sort of foreach mechanism. But that seems a bit tricky to me, seeing as how I use this approach of operating in pairs.

So what I would want to do is essentially replace:

DoOperation(list.Take(numberToProcess));

with

Thread lowerHalf = new Thread(() => => DoOperation(list.Take(numberToProcess/2)));

Thread lowerHalf = new Thread(() => => DoOperation(list.getRange(numberToProcess/2, numberToProcess));

lowerHalf.Start();
upperHalf.Start();

And this seems to get the work done, but it's VERY slow. Every iteration is slower than the previous one, and when I debug, the Thread view shows a growing list of Threads.

But I was under the impression that Threads terminated themselves upon completion? And yes, the threads do complete. The DoOperation() method is pretty much just a for loop.

So what am I not understanding here?

1
  • You could just do .Take(halfCount) for the lower half, then .Skip(halfCount) on the upper half. Commented Nov 9, 2016 at 13:04

2 Answers 2

5

Try Parallel.For It will save lot of work.

Sign up to request clarification or add additional context in comments.

2 Comments

I can't seem to get it to work. I try to run a loop that would trigger this threading behaviour 18 times, but after 1-7 (very fast) runs, the program seems to be stuck somewhere.
Seriously, this x 100000
2

To explain pranitkothari's answer a little bit more and give a different example you can use

list.AsParallel().ForAll(delegate([ListContainingType] item) {
    // do stuff to a single item here (whatever is done in DoOperation() in your code
    // except applied to a single item rather than several)
});

For instance, if I had a list string, it would be

List<String> list = new List<String>();
list.AsParallel().ForAll(delegate(String item) {
    // do stuff to a single item here (whatever is done in DoOperation() in your code
    // except applied to a single item rather than several)
});

This will let you perform an operation for each item in the list on a separate thread. It's simpler in that it handles all the "multi-threadedness" for you.

This is a good post that explains one of the differences in them

4 Comments

No luck so far with this approach. Lots of squiggly red lines. It says the type for the argument to the ForAll() call cannot be inferred. Is it because it is a custom object and not a primitive?
That shouldn't matter, as long as you replace [ListContainingType] with the type that is contained in the list, then it should work.
I did that. But don't have the code available to me at the moment. Will have a look later on. Maybe I made some other silly mistake.
Gotcha, when you get it, go ahead and update your question with what you've tried so we can see what exactly is happening.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.