简体   繁体   English

C#多线程同步

[英]C# Multithreading synchronization

I'm doing one program which needs multithreading synchronization. 我正在做一个需要多线程同步的程序。 Here are my methods: 这是我的方法:

    private void RunSimulation()
    {
        int sideSize = (int)Math.Ceiling((double)_simulationField.GetLength(0) / (double)MAX_SECTOR_SIZE);
        int threadsCount = sideSize * sideSize;
        sync = new bool[threadsCount + 1];
        for (int i = 0; i < sync.Length; i++)
        {
            sync[i] = false;
        }
            for (int sector = 0; sector <= threadsCount; sector++)
            {
                Thread sectorThread = new Thread(DoSector);
                sectorThread.IsBackground = true;
                sectorThread.Start(sector);
            }
    }

And: 和:

    private void DoSector(object obj)
    {
        int sector = (int)obj;
        Library.Pair sectorPosition = GetSectorPosition(sector);
        while (true)
        {

            for (int i = sectorPosition.X; i < sectorPosition.X + MAX_SECTOR_SIZE && i < _simulationField.GetLength(0); i++)
            {
                for (int j = sectorPosition.Y; j < sectorPosition.Y + MAX_SECTOR_SIZE && j < _simulationField.GetLength(1); j++)
                {
                    if (_simulationField[i, j] != null)
                    {
                        MoveCreature(_simulationField[i, j], i, j);
                    }
                }
            }
            sync[sector] = true;
            while (sync.Contains(false) && sector != sync.Length)
            {
                Thread.Sleep(10);
            }
            if (sector == sync.Length)
            {
                for (int i = 0; i < sync.Length; i++)
                {
                    sync[i] = true;
                }
            }
            Thread.Sleep(500);
        }
    }

So you can see that I'm using an array for the synchronization. 所以你可以看到我正在使用一个数组进行同步。 But I'm wandering is it good way to do this? 但我徘徊是不是这样做的好方法? Here the operation sync.Contains is with complexity O(n) and it's in loop...Is there a better way to do the synchronization? 这里的操作sync.Contains具有复杂度O(n)并且它处于循环中...是否有更好的方法来进行同步?

I think you want to use a Barrier here. 我想你想在这里使用障碍物 It's a synchronization object that allows threads to execute in phases. 它是一个允许线程分阶段执行的同步对象。 All threads must finish their computation in one phase before they are allowed to execute the next one. 所有线程必须在允许执行下一个阶段之前完成一个阶段的计算。 This is just what you need. 这正是您所需要的。

Using an array to sync is not thread safe, yet alone "sync.Contains(false)". 使用数组进行同步不是线程安全的,而是单独使用“sync.Contains(false)”。

What if two threads are running sync.Contains(false) at the same time and both come back false? 如果两个线程同时运行sync.Contains(false)并且两者都返回false会怎么样?

Also, I see your ThreadCount is sideSize squared. 另外,我看到你的ThreadCount是sideSize平方。 It is very inefficient to create lots of threads. 创建大量线程效率非常低。 the more threads you have, the more context switches you have. 你拥有的线程越多,你拥有的上下文切换就越多。 A context switch is about 8000 clock cycles. 上下文切换大约是8000个时钟周期。 Also, creating a thread costs about 100,000 clock cycles and each thread must have its own stack which is about 4MB by default. 此外,创建一个线程大约需要100,000个时钟周期,每个线程必须有自己的堆栈,默认情况下大约为4MB。

What you want is about as many threads as cores and each thread to pull from a queue and process its work. 你想要的是与核心一样多的线程以及从队列中提取并处理其工作的每个线程。

You're probably best off using the thread pool, but I also see your threads are in a while(true), which means they never end. 你可能最好使用线程池,但我也看到你的线程有一段时间(真实),这意味着它们永远不会结束。 The thread pool isn't meant for long running threads. 线程池不适用于长时间运行的线程。

Over all, I think your design needs some work. 总而言之,我认为您的设计需要一些工作。 Since you seem to be learning, I would recommend a design that allows the use of the thread pool, instead of while(true) loops. 由于您似乎在学习,我建议使用允许使用线程池的设计,而不是while(true)循环。

Someone else might be able to give you a "name" for a known design pattern that would work for you, but I'm not up on names for designs patterns. 其他人可能会为您提供一个适合您的已知设计模式的“名称”,但我不知道设计模式的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM