使用 Parallel.For 中的输出参数调用函数

Call functions with output parameters in Parallel.For

如何修改带有输出参数的函数的方法#2?方法#1 和方法#2 具有相同的功能,但给出不同的结果。我不知道为什么。带有输出参数或 Mpir.NET 类型或 Parallel.For.

中的 HashSet 的调用函数可能有问题
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Mpir.NET;

namespace ConsoleApplication4
{
    class Program
    {
        public struct numbers
        {
            public numbers(mpz_t p, mpz_t q)
            {
                this.q = q;
                this.p = p;
            }
            mpz_t q;
            mpz_t p;
        };

        static void Main(string[] args)
        {
            Int32 arraySize = 100;
            HashSet<numbers> pairs = new HashSet<numbers>();
            HashSet<numbers> unique = new HashSet<numbers>();

            mpz_t[] numbersArray = new mpz_t[arraySize];

            for (Int32 i = 0; i < arraySize; i++)
            {
                numbersArray[i] = i*i+i+1; 
            }

            //Methode #1
            for (Int32 j = 0; j < arraySize; j++)
            { 
                mpz_t p, q;

                for (Int32 m = 0; m < 16; m++)
                {
                    if (checkDivisible(numbersArray[j]*m, out p, out q))
                        pairs.Add(new numbers(p, q));

                    unique.Add(new numbers(p, q));
                }



            }
            Console.WriteLine("Count pairs {0}\t{1}", pairs.Count(), unique.Count());

            pairs = new HashSet<numbers>();
            unique = new HashSet<numbers>();
            //Methode #2
            Parallel.For(0, arraySize, j => 
            {
                mpz_t p, q;

                for (Int32 m = 0; m < 16; m++)
                {
                    if (checkDivisible(numbersArray[j]*m, out p, out q))
                        pairs.Add(new numbers(p, q));

                    unique.Add(new numbers(p, q));
                }


            }
            );

            Console.WriteLine("Count pairs {0}\t{1}", pairs.Count(), unique.Count());


            Console.ReadKey();
        }

        private static Boolean checkDivisible(mpz_t n, out mpz_t p, out mpz_t q)
        {
            p = 1; 
            q = 1;    

            for (Int32 i = 2; i < n; i++)
            {
                if (n % i == 0)
                {
                    q = i;
                    p = n / i;
                    return true;
                }
            }

            return false;
        }
    }
}

下面是元组代码。同样的问题:(

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Mpir.NET;

namespace ConsoleApplication4
{
    class Program
    {
        public struct numbers
        {
            public numbers(mpz_t p, mpz_t q)
            {
                this.q = q;
                this.p = p;
            }
            mpz_t q;
            mpz_t p;
        };

        static void Main(string[] args)
        {
            Int32 arraySize = 100;
            HashSet<numbers> pairs = new HashSet<numbers>();
            HashSet<numbers> unique = new HashSet<numbers>();

            mpz_t[] numbersArray = new mpz_t[arraySize];

            for (Int32 i = 0; i < arraySize; i++)
            {
                numbersArray[i] = i*i+i+1; 
            }

            //Methode #1
            for (Int32 j = 0; j < arraySize; j++)
            { 
                mpz_t p = 1, q = 1;

                for (Int32 m = 0; m < 16; m++)
                {
                    Tuple<Boolean, mpz_t, mpz_t> t = checkDivisible(numbersArray[j] * m);
                    if (t.Item1 == true)
                        pairs.Add(new numbers(t.Item2, t.Item3));

                    unique.Add(new numbers(t.Item2, t.Item3));
                }



            }
            Console.WriteLine("Count pairs {0}\t{1}", pairs.Count(), unique.Count());

            pairs = new HashSet<numbers>();
            unique = new HashSet<numbers>();
            //Methode #2
            Parallel.For(0, arraySize, j => 
            {
                mpz_t p = 1, q = 1;

                for (Int32 m = 0; m < 16; m++)
                {
                    Tuple<Boolean, mpz_t, mpz_t> t = checkDivisible(numbersArray[j] * m);
                    if (t.Item1 == true)
                        pairs.Add(new numbers(t.Item2, t.Item3));

                    unique.Add(new numbers(t.Item2, t.Item3));
                }


            }
            );

            Console.WriteLine("Count pairs {0}\t{1}", pairs.Count(), unique.Count());


            Console.ReadKey();
        }

        private static Tuple<Boolean, mpz_t, mpz_t> checkDivisible(mpz_t n)
        {
            mpz_t p = 1; 
            mpz_t q = 1;    

            for (Int32 i = 2; i < n; i++)
            {
                if (n % i == 0)
                {
                    q = i;
                    p = n / i;
                    return new Tuple<Boolean, mpz_t, mpz_t>(true, p, q);
                }
            }

            return new Tuple<Boolean, mpz_t, mpz_t>(false, p, q);;
        }
    }
}

为了解决并行 lopp 中哈希表的问题,对线程部分使用 ConcurrentDictionary