parallel.for

BlockingCollection with Parallel.For hangs?

陌路散爱 提交于 2021-02-08 19:43:56
问题 I'm playing around with BlockingCollection to try to understand them better, but I'm struggling to understand why my code hangs when it finishes processing all my items when I use a Parallel.For I'm just adding a number to it (producer?): var blockingCollection = new BlockingCollection<long>(); Task.Factory.StartNew(() => { while (count <= 10000) { blockingCollection.Add(count); count++; } }); Then I'm trying to process (Consumer?): Parallel.For(0, 5, x => { foreach (long value in

BlockingCollection with Parallel.For hangs?

浪子不回头ぞ 提交于 2021-02-08 19:43:53
问题 I'm playing around with BlockingCollection to try to understand them better, but I'm struggling to understand why my code hangs when it finishes processing all my items when I use a Parallel.For I'm just adding a number to it (producer?): var blockingCollection = new BlockingCollection<long>(); Task.Factory.StartNew(() => { while (count <= 10000) { blockingCollection.Add(count); count++; } }); Then I'm trying to process (Consumer?): Parallel.For(0, 5, x => { foreach (long value in

BlockingCollection with Parallel.For hangs?

房东的猫 提交于 2021-02-08 19:43:20
问题 I'm playing around with BlockingCollection to try to understand them better, but I'm struggling to understand why my code hangs when it finishes processing all my items when I use a Parallel.For I'm just adding a number to it (producer?): var blockingCollection = new BlockingCollection<long>(); Task.Factory.StartNew(() => { while (count <= 10000) { blockingCollection.Add(count); count++; } }); Then I'm trying to process (Consumer?): Parallel.For(0, 5, x => { foreach (long value in

BlockingCollection with Parallel.For hangs?

拜拜、爱过 提交于 2021-02-08 19:42:39
问题 I'm playing around with BlockingCollection to try to understand them better, but I'm struggling to understand why my code hangs when it finishes processing all my items when I use a Parallel.For I'm just adding a number to it (producer?): var blockingCollection = new BlockingCollection<long>(); Task.Factory.StartNew(() => { while (count <= 10000) { blockingCollection.Add(count); count++; } }); Then I'm trying to process (Consumer?): Parallel.For(0, 5, x => { foreach (long value in

Increase Number of running thread in Parallel.For

我的未来我决定 提交于 2021-02-05 08:52:07
问题 I have just did a sample for multithreading using This Link like below: Console.WriteLine("Number of Threads: {0}", System.Diagnostics.Process.GetCurrentProcess().Threads.Count); int count = 0; Parallel.For(0, 50000, options,(i, state) => { count++; }); Console.WriteLine("Number of Threads: {0}", System.Diagnostics.Process.GetCurrentProcess().Threads.Count); Console.ReadKey(); It gives me 15 thread before Parellel.For and after it gives me 17 thread only. So only 2 thread is occupy with

Why is this causing an ArgumentOutOfRangeException when using Parallel.For?

Deadly 提交于 2020-01-15 23:45:22
问题 I have had a go at writing something to hash numbers and check them against a list to see if a matching hash exists or not. I got this working fine using a for loop, I then decided to try speed things up by using Parallel.For - unfortunately this causes an ArgumentOutOfRangeException that i'm having trouble debugging. public class HashCompare { private string encryptedCardNumber; private Encrypter encrypter; private BlockingCollection<string> paraCardNumberList; public string Compare(string

Parallel.For items in a List by using ConcurrentQueue - will this work?

自作多情 提交于 2020-01-05 19:20:42
问题 I have a List<TaskClass> TaskList items that we can iterate over using a Parallel loop. The items in the list are sorted in a particular order as the TaskClass implements IComparable with its own CompareTo(object obj) method. Thus we need the items acted upon in sequential order. Note they do NOT have to complete in sequential order, just start in sequential. Thus TaskList[0] should be started first; then TaskList[1], TaskList[2], ... However, we don't care if TaskList[2] completes first, or

Threading with Parallel.For Adding Lists

不问归期 提交于 2019-12-12 02:49:53
问题 I am having trouble using the Parallel.For method. I am making a GET call to get back a list. Then I want to take that list and add it to the main list. I have tried addRange which is not thread safe, and will return the wrong data in the list. I have also tried to use ConcurrentBag which also does not get the right data. When I say does not get the right data I mean some of the data in the lists is either repeating or gets over written. Here is my code(updated): var thisLock = new Object();

Parallel.For with BigInteger calculations output different than For loop

蹲街弑〆低调 提交于 2019-12-07 17:47:44
问题 I have the following loop that runs a conversion from base95 to base10. I'm working with several thousand digit numbers, so BigIntegers are required. inst is the base95 string. Parallel.For(0, inst.Length, x => { result += BigInteger.Pow(95, x) * (inst[x] - 32); }); If I work with base95 strings of about 200 characters or less, it works perfectly fine and outputs the same result that a normal for loop would. However, once I start increasing the size of the base95 string, the parallel's output

Parallel.For not utilising all cores

丶灬走出姿态 提交于 2019-12-01 14:43:52
问题 I'm doing heavy mathematical computations using Math.Net Numerics parallely inside Parallel.For block. When I run code in my local system with 4 cores(2*2), it's using all 4 cores. But when I run same code in our dev server with 8 cores(4*2), it's using only 4 cores. I've tried setting MaxDegreeOfParallism,but couldn't help. Any idea why all cores are not being utilised. Below is sample code. Parallel.For(0,10000,(i)=> { // heavy math computations using matrices }); 回答1: From MSDN By default,