parallel-extensions

When to dispose CancellationTokenSource?

人走茶凉 提交于 2019-11-27 02:58:49
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 hand, if you look at the samples listed on the MSDN article Cancellation in Managed Threads , only one code snippet disposes of the token. What is the proper way to dispose of it in code? You cannot wrap code starting your parallel task with using if you do not wait for it. And it makes sense to have cancellation only if you do not wait. Of course you

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

只谈情不闲聊 提交于 2019-11-26 23:48:31
问题 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 constantly mentioned everywhere lately: Reactive Framework, PLINQ (Parallel LINQ), TPL (Task Parallel Library) and Parallel Extensions. Now they all seem to be Microsoft products and they all seem to target asynchronous or parallel programming

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

余生长醉 提交于 2019-11-26 22:45:12
问题 So for example: ConcurrentDictionary<string,Payload> itemCache = GetItems(); foreach(KeyValuePair<string,Payload> kvPair in itemCache) { if(TestItemExpiry(kvPair.Value)) { // Remove expired item. Payload removedItem; itemCache.TryRemove(kvPair.Key, out removedItem); } } Obviously with an ordinary Dictionary this will throw an exception because removing items changes the dictionary's internal state during the life of the enumeration. It's my understanding that this is not the case for a

Parallel Sort Algorithm

▼魔方 西西 提交于 2019-11-26 21:48:28
I'm looking for a simple implementation of a parallelized (multi-threaded) sort algorithm in C# that can operate on List<T> or Arrays, and possibly using Parallel Extensions but that part isn't strictly necessary. Edit: Frank Krueger provides a good answer, however I wish to convert that example to one that doesn't use LINQ. Also note that Parallel.Do() seems to have been superceded by Parallel.Invoke() . Thanks. Frank Krueger From "The Darkside" in his article Parallel Extensions to the .Net Framework we have this parallel extensions version of quicksort: (Edit: Since the link is now dead,

What does MaxDegreeOfParallelism do?

﹥>﹥吖頭↗ 提交于 2019-11-26 20:51:04
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 no control over what kind of processor cores are available where my code runs, but is there some settings I can change with MaxDegreeOfParallelism that will probably run less operations simultaneously and not result in timeouts? I can increase timeouts but it isnt a good solution, if on lower CPU I can process less operations simultaneously, that

Save time with parallel FOR loop

梦想的初衷 提交于 2019-11-26 12:53:38
问题 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] * factor; } } public static void MultiplicateArray(double[] arrayToChange, double[] multiplication) { for (int i = 0; i < arrayToChange.Length; i++) { arrayToChange[i] = arrayToChange[i] * multiplication[i]; } } public static void MultiplicateArray(double[] arrayToChange, double[,]

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

。_饼干妹妹 提交于 2019-11-26 11:48:50
问题 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 stuff); foreach(feed in feeds) { read_from_web(feed) parse(feed) } So I want to implement concurrency and couldn\'t decide if I should basically use ThreadPools to process with worker threads or just rely on TPL to get it sorted. ThreadPools for

When to dispose CancellationTokenSource?

荒凉一梦 提交于 2019-11-26 09:06:52
问题 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 hand, if you look at the samples listed on the MSDN article Cancellation in Managed Threads, only one code snippet disposes of the token. What is the proper way to dispose of it in code? You cannot wrap code starting your parallel task with using if

Parallel Sort Algorithm

自古美人都是妖i 提交于 2019-11-26 08:03:51
问题 I\'m looking for a simple implementation of a parallelized (multi-threaded) sort algorithm in C# that can operate on List<T> or Arrays, and possibly using Parallel Extensions but that part isn\'t strictly necessary. Edit: Frank Krueger provides a good answer, however I wish to convert that example to one that doesn\'t use LINQ. Also note that Parallel.Do() seems to have been superceded by Parallel.Invoke() . Thanks. 回答1: From "The Darkside" in his article Parallel Extensions to the .Net

Parallel.ForEach vs Task.Factory.StartNew

夙愿已清 提交于 2019-11-26 00:35:36
问题 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<Item>(items, item => DoSomething(item)); vs foreach(var item in items) { Task.Factory.StartNew(() => DoSomething(item)); } 回答1: The first is a much better option. Parallel.ForEach, internally, uses a Partitioner<T> to distribute your collection into work items. It will not do one task per item, but rather