首页 > 解决方案 > 为什么不能得到我传递给 Thread 的矩阵?

问题描述

我想做一些基本的事情:获取一个矩阵,并使用多个线程对其进行迭代。例如,如果我有一个 20x20 大小的矩阵,并且我有 4 个线程,那么第一个线程必须迭代 5x20 大小的矩阵,第二个线程必须迭代相同的大小,但从第 6 行到第 10 行, 等等。但是,我的程序只有第一部分,第二、第三、第四个线程看不到它们的子矩阵。为什么?谁能帮我这个?

using System;
using System.Diagnostics;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace multi_thred_test
{
    class thredData 
    {
        public int my_simulation_size;
        public int[,] my_simulation;
        public string my_path;
        public int my_nrOfAvailableThreads;
        public int my_oneThreeadSubsimulationSize;

        public thredData(ref int simsize, ref int[,] simu, ref string path, ref int availabelthrd, ref int subsimsize )
        {
            my_simulation_size = simsize;
            my_simulation = new int[simsize, simsize];
            for (int i = 0; i < simsize; i++)
            {
                for (int j = 0; j < simsize; j++)
                {
                    my_simulation[i, j] = simu[i, j];
                }
            }
            my_path = path;
            my_nrOfAvailableThreads = availabelthrd;
            my_oneThreeadSubsimulationSize = subsimsize;
        }

    }
    
    class Program
    {
        public static int simulation_size = 20;
        public static int[,] simulation = new int[20, 20];
        public static string path = Directory.GetCurrentDirectory();
        public static int nrOfAvailableThreads = Environment.ProcessorCount;
        public static int oneThreeadSubsimulationSize = simulation_size / nrOfAvailableThreads;
        public static thredData tmp;

        public static void Main(string[] args)
        {
            for (int idx = 0; idx < simulation_size; idx++)
            {
                for (int jdx = 0; jdx < simulation_size; jdx++)
                {
                    simulation[idx, jdx] = idx * 100 + jdx;
                }
            }
            
            StreamWriter sw = new StreamWriter(path + "_matrix.txt");
            for (int idx = 0; idx < simulation_size; idx++)
            {
                for (int jdx = 0; jdx < simulation_size; jdx++)
                {
                    sw.Write(simulation[idx, jdx]+" ");
                }
                sw.WriteLine(" ");
            }
            sw.Close();

            tmp = new thredData(ref simulation_size, ref simulation, ref path, ref nrOfAvailableThreads, ref oneThreeadSubsimulationSize);

            //generate threads
            for (int thrdnr = 0; thrdnr < nrOfAvailableThreads; thrdnr++)
            {
                Thread newThread = new Thread(ThreadMethod);
                newThread.Name = Convert.ToString(thrdnr);
                newThread.Start();
            }

            Console.ReadKey();
        }

        private static void ThreadMethod()
        {
            Thread thr = Thread.CurrentThread;
            int simulationSize = Convert.ToInt32(thr.Name);

            int thrd_simulation_size;
            int[,] thrd_simulation;
            string thrd_path;
            int thrd_nrOfAvailableThreads;
            int thrd_oneThreeadSubsimulationSize;

            lock (tmp)
            {
                thrd_simulation_size = tmp.my_simulation_size;
                thrd_simulation = new int[thrd_simulation_size, thrd_simulation_size];

                for (int i = 0; i < tmp.my_simulation_size; i++)
                {
                    for (int j = 0; j < tmp.my_simulation_size; j++)
                    {
                        thrd_simulation[i, j] = tmp.my_simulation[i, j];
                    }
                }
                thrd_path = tmp.my_path;
                thrd_nrOfAvailableThreads = tmp.my_nrOfAvailableThreads;
                thrd_oneThreeadSubsimulationSize = tmp.my_oneThreeadSubsimulationSize;
            }

            for (int i = thrd_simulation_size * simulationSize; i < thrd_oneThreeadSubsimulationSize; i++)
             {
             for (int j = 0; j < thrd_simulation_size; j++)
             {
                 Console.Write(thr.Name +":"+ thrd_simulation[i,j] + " ");
             }
            Console.Write("\n");
            }
            
        }
    }
}

标签: c#multithreadingmatrix

解决方案


问题解决了,日志部分出错了:

            for (int i = thrd_oneThreeadSubsimulationSize * simulationSize; i < (thrd_oneThreeadSubsimulationSize * simulationSize) +thrd_oneThreeadSubsimulationSize; i++)
            {
                for (int j = 0; j < thrd_simulation_size; j++)
                {
                    Console.Write(thr.Name + ":" + thrd_simulation[i, j] + " ");
                }
                Console.Write("\n");
            }

推荐阅读