Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between BlockingCollection and TaskScheduler

Tags:

c#

I was seeking to parallelize some work on my project and I used Parallel.ForEach first and it worked good enough for the mean time, then the lead dev came up and said that he didn't really like this and wanted to change that.

Then I decided to do a TaskScheduler along a TaskFactory to batch the work, work flawless and fast, again he didn't like it and find it overkill and old, he said I should use BlockingCollection with Consumer/Producer pattern, he started to implement this and I don't see the advantage over a TaskScheduler. Maybe for maintenance problems, or to keep the complexity to something acceptable, I don't know.

Here some context, the work is transforming data from memory, the data do not need to have a synchronization context, each operations can be taken separately, no IO bounds and all the code is thread-safe.

So, what the differences between the BlockingCollection and TaskScheduler for a parallel work ?

like image 361
Maillful Avatar asked Apr 25 '26 07:04

Maillful


1 Answers

So, what the differences between the BlockingCollection and TaskScheduler for a parallel work ?

As the comments said, they are as different as night and oranges. BlockingCollection<T> ist the collection you find in the Namespace System.Collections.Concurrent. Wich consists basically of everything from System.Collections.Generic they could fully protect from race conditions via the use of lock statement in every sensible place.

If you got a colletion you want outside this, they could not proof the collection fully against Race Conditions from the inside. Only the code using those collections can avoid race conditions (a likely reason why Array and List<T> do not have a concurrent counterpart).

he didn't really like this

again he didn't like it and find it overkill and old, he said I should use BlockingCollection with Consumer/Producer pattern

Then the difference does not realy mater either way. The choice is out of your hand.

There are either requirements elsewhere you do not (yet) know. Or "old dogs do not learn new tricks" applies.

like image 174
Christopher Avatar answered Apr 27 '26 19:04

Christopher



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!