How to make threads go through a gate in order using C#

前端 未结 1 1961
失恋的感觉
失恋的感觉 2021-01-24 19:25

I have three threads and some part of the code can run in parallel, some parts are locked(only one thread at the time). However one lock needs to only let them in in order. Sinc

1条回答
  •  [愿得一人]
    2021-01-24 19:52

    You could use a combination of Barrier and AutoResetEvent to achieve this.

    Firstly, you use Barrier.SignalAndWait() to ensure that all the threads reach a common point before proceeding. This common point is the point at which you want the threads to execute some code in order.

    Then you use numberOfThreads-1 AutoResetEvents to synchronise the threads. The first thread doesn't need to wait for any other thread, but after it has finished it should signal the event that the next thread is waiting on.

    The middle thread (or threads if more than 3 threads total) needs to wait for the previous thread to signal the event that tells it to proceed. After it has finished, the middle thread should signal the event that the next thread is waiting on.

    The last thread needs to wait for the previous thread to signal the event that tells it to proceed. Since it is the last thread, it does not need to signal an event to tell the next thread to proceed.

    Finally, you resync the threads with another call to Barrier.SignalAndWait().

    This is easiest to show via a sample console app. If you run it, you'll see that the work that should be done by the threads in order (prefixed with the letter "B" in the output) is indeed always in order, while the other work (prefixed with the letter "A") is executed in a random order.

    using System;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace Demo
    {
        public static class Program
        {
            public static void Main()
            {
                using (Barrier barrier = new Barrier(3))
                using (AutoResetEvent t2 = new AutoResetEvent(false))
                using (AutoResetEvent t3 = new AutoResetEvent(false))
                {
                    Parallel.Invoke
                    (
                        () => worker(1, barrier, null, t2),
                        () => worker(2, barrier, t2, t3),
                        () => worker(3, barrier, t3, null)
                    );
                }
            }
    
            private static void worker(int threadId, Barrier barrier, AutoResetEvent thisThreadEvent, AutoResetEvent nextThreadEvent)
            {
                Random rng = new Random(threadId);
    
                for (int i = 0; i < 1000; ++i)
                {
                    doSomething(threadId, rng); // We don't care what order threads execute this code.
    
                    barrier.SignalAndWait(); // Wait for all threads to reach this point.
    
                    if (thisThreadEvent != null)   // If this thread is supposed to wait for a signal
                        thisThreadEvent.WaitOne(); // before proceeding, then wait for it.
    
                    doWorkThatMustBeDoneInThreadOrder(threadId);
    
                    if (nextThreadEvent != null) // If this thread is supposed to raise a signal to indicate
                        nextThreadEvent.Set();   // that the next thread should proceed, then raise it.
    
                    barrier.SignalAndWait(); // Wait for all threads to reach this point.
                }
            }
    
            private static void doWorkThatMustBeDoneInThreadOrder(int threadId)
            {
                Console.WriteLine("   B" + threadId);
                Thread.Sleep(200); // Simulate work.
            }
    
            private static void doSomething(int threadId, Random rng)
            {
                for (int i = 0; i < 5; ++i)
                {
                    Thread.Sleep(rng.Next(50)); // Simulate indeterminate amount of work.
                    Console.WriteLine("A" + threadId);
                }
            }
        }
    }
    

    0 讨论(0)
提交回复
热议问题