Turn based synchronization between threads

Posted by Amarus on Stack Overflow See other posts from Stack Overflow or by Amarus
Published on 2013-11-07T15:51:49Z Indexed on 2013/11/07 15:53 UTC
Read the original article Hit count: 133

I'm trying to find a way to synchronize multiple threads having the following conditions: * There are two types of threads: 1. A single "cyclic" thread executing an infinite loop to do cyclic calculations 2. Multiple short-lived threads not started by the main thread * The cyclic thread has a sleep duration between each cycle/loop iteration * The other threads are allowed execute during the inter-cycle sleep of the cyclic thread: - Any other thread that attempts to execute during an active cycle should be blocked - The cyclic thread will wait until all other threads that are already executing to be finished

Here's a basic example of what I was thinking of doing:

// Somewhere in the code:
ManualResetEvent manualResetEvent = new ManualResetEvent(true); // Allow Externally call
CountdownEvent countdownEvent = new CountdownEvent(1); // Can't AddCount a CountdownEvent with CurrentCount = 0

void ExternallyCalled()
{
    manualResetEvent.WaitOne(); // Wait until CyclicCalculations is having its beauty sleep

    countdownEvent.AddCount(); // Notify CyclicCalculations that it should wait for this method call to finish before starting the next cycle

    Thread.Sleep(1000); // TODO: Replace with actual method logic

    countdownEvent.Signal(); // Notify CyclicCalculations that this call is finished
}

void CyclicCalculations()
{
    while (!stopCyclicCalculations)
    {
        manualResetEvent.Reset(); // Block all incoming calls to ExternallyCalled from this point forward

        countdownEvent.Signal(); // Dirty workaround for the issue with AddCount and CurrentCount = 0
        countdownEvent.Wait(); // Wait until all of the already executing calls to ExternallyCalled are finished

        countdownEvent.Reset(); // Reset the CountdownEvent for next cycle.

        Thread.Sleep(2000); // TODO: Replace with actual method logic

        manualResetEvent.Set(); // Unblock all threads executing ExternallyCalled

        Thread.Sleep(1000); // Inter-cycles delay
    }
}

Obviously, this doesn't work. There's no guarantee that there won't be any threads executing ExternallyCalled that are in between manualResetEvent.WaitOne(); and countdownEvent.AddCount(); at the time the main thread gets released by the CountdownEvent.

I can't figure out a simple way of doing what I'm after, and almost everything that I've found after a lengthy search is related to producer/consumer synchronization which I can't apply here.

© Stack Overflow or respective owner

Related posts about c#

Related posts about multithreading