在c#中使用多线程从TwinCat遍历非二叉树状结构

本文关键字:遍历 二叉树 结构 TwinCat 多线程 | 更新日期: 2023-09-27 18:15:11

我正试图通过ADS接口优化我用来在TwinCat 3中找到标记符号的搜索算法。这个问题与TwinCat无关,所以不要被吓跑。

问题:符号不会立即加载。我认为TwinCatAds库使用延迟加载。符号具有非二叉非平衡树的树状结构。

解决方案:您可以打开多个流到ADS,并在多个线程中处理这些流。

问题是,我将第一层符号除以处理器内核的数量。因为树是不平衡的,一些线程完成得比其他线程快。因此,我需要一个更好的解决方案,如何在线程之间分配工作。

PS:我不能使用Parallel.ForEach()。由于流的存在,它会导致与单线程解决方案相同或更多的时间。

我的测试代码看起来是这样的,它只是计算一个大项目的所有符号。

using TwinCAT.Ads;
using System.Threading;
using System.IO;
using System.Diagnostics;
using System.Collections;

namespace MultipleStreamsTest
{
class Program
{
    static int numberOfThreads = Environment.ProcessorCount;
    static TcAdsClient client;
    static TcAdsSymbolInfoLoader symbolLoader;
    static TcAdsSymbolInfoCollection[] collection = new TcAdsSymbolInfoCollection[numberOfThreads];
    static int[] portionResult = new int[numberOfThreads];
    static int[] portionStart = new int[numberOfThreads];
    static int[] portionStop = new int[numberOfThreads];
    static void Connect()
    {
        client = new TcAdsClient();
        client.Connect(851);
        Console.WriteLine("Conected ");
    }
    static void Main(string[] args)
    {
        Connect();
        symbolLoader = client.CreateSymbolInfoLoader();
        CountAllOneThread();
        CountWithMultipleThreads();
        Console.ReadKey();
    }        
    static public void CountAllOneThread()
    {
        Stopwatch stopwatch = new Stopwatch();
        int index = 0;
        stopwatch.Start();
        Console.WriteLine("Counting with one thread...");
        //Count all symbols
        foreach (TcAdsSymbolInfo symbol in symbolLoader)
        {                
            index++;
        }
        stopwatch.Stop();
        //Output
        Console.WriteLine("Counted with one thred " + index + " symbols in " + stopwatch.Elapsed);
    }
    static public int countRecursive(TcAdsSymbolInfo symbol)
    {
        int i = 0;
        TcAdsSymbolInfo subSymbol = symbol.FirstSubSymbol;
        while (subSymbol != null)
        {
            i = i + countRecursive(subSymbol);
            subSymbol = subSymbol.NextSymbol;
            i++;
        }
        return i;
    }
    static public void countRecursiveMultiThread(object portionNum)
    {
        int portionNumAsInt = (int)portionNum;
        for (int i = portionStart[portionNumAsInt]; i <= portionStop[portionNumAsInt]; i++)
        {
                portionResult[portionNumAsInt] += countRecursive(collection[portionNumAsInt][i]);//Collection Teil 
        }
    }
    static public void CountWithMultipleThreads()
    {
        Stopwatch stopwatch = new Stopwatch();
        int sum = 0;
        stopwatch.Start();
        Console.WriteLine("Counting with multiple thread...");
        for (int i = 0; i < numberOfThreads; i++)
        {
            collection[i] = symbolLoader.GetSymbols(true);
        }
        int size = (int)(collection[0].Count / numberOfThreads);
        int rest = collection[0].Count % numberOfThreads;
        int m = 0;
        for (; m < numberOfThreads; m++)
        {
            portionStart[m] = m * size;
            portionStop[m] = portionStart[m] + size - 1;
        }
        portionStop[m - 1] += rest;
        Thread[] threads = new Thread[numberOfThreads];
        for (int i = 0; i < numberOfThreads; i++)
        {
            threads[i] = new Thread(countRecursiveMultiThread);
            threads[i].Start(i);
            Console.WriteLine("Thread #" + threads[i].ManagedThreadId + " started, fieldIndex: " + i);
        }
        //Check when threads finishing:
        int threadsFinished = 0;
        bool[] threadFinished = new bool[numberOfThreads];
        int x = 0;
        while (true)
        {
            if (threads[x].Join(10) && !threadFinished[x] )
            {
                Console.WriteLine("Thread #" + threads[x].ManagedThreadId + " finished ~ at: " + stopwatch.Elapsed);
                threadsFinished++;
                threadFinished[x] = true;                    
            }
            x++;
            x = x % numberOfThreads;
            if (threadsFinished == numberOfThreads) break;
            Thread.Sleep(50);
        }            
        foreach (int n in portionResult)
        {
            sum += n;
        }
        sum += collection[0].Count;
        stopwatch.Stop();
        //Output
        Console.WriteLine("Counted with multiple threds in Collection " + sum + " symbols " + " in " + stopwatch.Elapsed);
        for (int i = 0; i < numberOfThreads; i++)
        {
            Console.WriteLine("#" + i + ": " + portionResult[i]);
        }
    }
}
}

控制台输出:

如果您试图运行代码,请使用TwinCat。广告版本4.0.17.0(我正在使用)。他们在NuGet的新版本中破坏了一些东西。

在c#中使用多线程从TwinCat遍历非二叉树状结构

创建线程池并跟踪正在运行和空闲的线程状态。在每个分支检查是否有空闲线程,是否有分配线程给子分支