Questions tagged [parallel-extensions]

Parallel Extensions is a managed concurrency library included in the .NET 4.0 release.

Parallel Extensions is a managed concurrency library included in the .NET 4.0 release. It is composed of two parts: Parallel LINQ (PLINQ) and Task Parallel Library (TPL).

127 questions
273
votes
4 answers

Parallel.ForEach vs Task.Factory.StartNew

What is the difference between the below code snippets? Won't both be using threadpool threads? For instance if I want to call a function for each item in a collection, Parallel.ForEach(items, item => DoSomething(item)); vs foreach(var item…
stackoverflowuser
  • 20,984
  • 28
  • 65
  • 89
180
votes
7 answers

When to dispose CancellationTokenSource?

The class CancellationTokenSource is disposable. A quick look in Reflector proves usage of KernelEvent, a (very likely) unmanaged resource. Since CancellationTokenSource has no finalizer, if we do not dispose it, the GC won't do it. On the other…
79
votes
6 answers

Should i use ThreadPools or Task Parallel Library for IO-bound operations

In one of my projects that's kinda an aggregator, I parse feeds, podcasts and so from the web. If I use sequential approach, given that a large number of resources, it takes quite a time to process all of them (because of network issues and similar…
68
votes
2 answers

How do Reactive Framework, PLINQ, TPL and Parallel Extensions relate to each other?

At least since the release of .NET 4.0, Microsoft seems to have put a lot of effort in support for parallel and asynchronous programming and it seems a lot of APIs and libraries around this have emerged. Especially the following fancy names are…
bitbonk
  • 45,662
  • 32
  • 173
  • 270
55
votes
5 answers

What does MaxDegreeOfParallelism do?

I am using Parallel.ForEach and I am doing some database updates, now without setting MaxDegreeOfParallelism , a dual core processor machine results in sql client timeouts, where else quad core processor machine somehow does not timeout. Now I have…
50
votes
5 answers

Save time with parallel FOR loop

I have a question concerning parallel for loops. I have the following code: public static void MultiplicateArray(double[] array, double factor) { for (int i = 0; i < array.Length; i++) { array[i] = array[i] *…
tro
  • 804
  • 2
  • 10
  • 22
38
votes
4 answers

Can I remove items from a ConcurrentDictionary from within an enumeration loop of that dictionary?

So for example: ConcurrentDictionary itemCache = GetItems(); foreach(KeyValuePair kvPair in itemCache) { if(TestItemExpiry(kvPair.Value)) { // Remove expired item. itemCache.TryRemove(kvPair.Key,…
redcalx
  • 7,398
  • 3
  • 45
  • 95
35
votes
6 answers

List thread safety

I am using the below code var processed = new List(); Parallel.ForEach(items, item => { processed.Add(SomeProcessingFunc(item)); }); Is the above code thread safe? Is there a chance of processed list getting corrupted? Or should i use a…
stackoverflowuser
  • 20,984
  • 28
  • 65
  • 89
25
votes
5 answers

Parallel Sort Algorithm

I'm looking for a simple implementation of a parallelized (multi-threaded) sort algorithm in C# that can operate on List or Arrays, and possibly using Parallel Extensions but that part isn't strictly necessary. Edit: Frank Krueger provides a good…
redcalx
  • 7,398
  • 3
  • 45
  • 95
21
votes
3 answers

Throw Exception inside a Task - "await" vs Wait()

static async void Main(string[] args) { Task t = new Task(() => { throw new Exception(); }); try { t.Start(); t.Wait(); } catch (AggregateException e) { // When waiting…
19
votes
4 answers

What is the purpose of BlockingCollection(Of T)

I´m trying to understand the purpose of BlockingCollection in the context of the new Parallel Stacks on .NET 4. The MSDN documentation says: BlockingCollection is used as a wrapper for an IProducerConsumerCollection instance, allowing removal…
Iñaki Elcoro
  • 2,085
  • 18
  • 32
15
votes
3 answers

Parallel.ForEach keeps spawning new threads

While I was using Parallel.ForEach in my program, I found that some threads never seemed to finish. In fact, it kept spawning new threads over and over, a behaviour that I wasn't expecting and definitely don't want. I was able to reproduce this…
Astrotrain
  • 3,534
  • 1
  • 19
  • 32
13
votes
1 answer

How to PLINQ an existing LINQ query with Joins?

I'm using LINQ to compare two DataSets with each other to create new rows and update existing. I've noticed that the complete comparison lasts ~1,5 hours and only one of the two cores is busy(Task-Manager is 50-52% CPU Usage). I must admit that I'm…
Tim Schmelter
  • 411,418
  • 61
  • 614
  • 859
13
votes
4 answers

Parallel.Foreach spawning way too many threads

The problem Although the code about which I will talk here I wrote in F#, it is based on the .NET 4 framework, not specifically depending on any particularity of F# (at least it seems so!). I have some pieces of data on my disk that I should update…
12
votes
6 answers

Unit testing concurrent software - what do you do?

As software gets more and more concurrent, how do you handle testing the core behaviour of the type with your unit tests (not the parallel behaviour, just the core behaviour)? In the good old days, you had a type, you called it, and you checked…
1
2 3
8 9