none
多线程处理数组问题 RRS feed

  • 问题

  • 假设有a(),b(),c()两个数组各有10元素,我希望有10个线程执行c(1)=a(1)+b(1)………………C(10)=a(10)+b(10),应该怎样写,thanks!
    2013年3月8日 10:24

答案

  • 可以参考:

    class Program
        {
            static void Add(int num1int num2int index,int[]c)
            {
                c[index= num1 + num2;
            }
     
            static void Main(string[] args)
            {
                //生成数组
                int[] a = new int[10];
                int[] b = new int[10];
                int[] c = new int[10];
     
                //是否完成
                AutoResetEvent[] flags = new AutoResetEvent[10];
     
                for (int i = 1i < 11i++)
                {
                    a[i-1= b[i-1= i;
                }
     
                for (int i = 0i < 10i++)
                {
                    int index = i;
                   flags[index= new AutoResetEvent(false);
     
                    Thread th = new Thread
                        (
                        () =>
                        {
                            Add(a[index], b[index], indexc);
                            //计算完毕,标记完成
                            flags[index].Set();
                        }
                        );
                    th.IsBackground = true;
                    th.Start();
                }
     
                //主线程等候
                WaitHandle.WaitAll(flags);
                //输出结果
                foreach (var item in c)
                {
                    Console.WriteLine(item);
                }
            }
        }

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats

    2013年3月8日 12:27
    版主

全部回复

  • 可以参考:

    class Program
        {
            static void Add(int num1int num2int index,int[]c)
            {
                c[index= num1 + num2;
            }
     
            static void Main(string[] args)
            {
                //生成数组
                int[] a = new int[10];
                int[] b = new int[10];
                int[] c = new int[10];
     
                //是否完成
                AutoResetEvent[] flags = new AutoResetEvent[10];
     
                for (int i = 1i < 11i++)
                {
                    a[i-1= b[i-1= i;
                }
     
                for (int i = 0i < 10i++)
                {
                    int index = i;
                   flags[index= new AutoResetEvent(false);
     
                    Thread th = new Thread
                        (
                        () =>
                        {
                            Add(a[index], b[index], indexc);
                            //计算完毕,标记完成
                            flags[index].Set();
                        }
                        );
                    th.IsBackground = true;
                    th.Start();
                }
     
                //主线程等候
                WaitHandle.WaitAll(flags);
                //输出结果
                foreach (var item in c)
                {
                    Console.WriteLine(item);
                }
            }
        }

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats

    2013年3月8日 12:27
    版主
  • 可以使用这个在线工具将C#转换为VB代码:http://www.developerfusion.com/tools/convert/csharp-to-vb/。

    Shanks Zen
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2013年3月11日 5:14
    版主
  • thanks!
    2013年3月14日 11:21