Synchronizing two threads with AutoResetEvent
Asked Answered
C

1

10

I'm trying to implement AutoResetEvent. For the purpose I use a very simple class :

public class MyThreadTest
{
    static readonly AutoResetEvent thread1Step = new AutoResetEvent(false);
    static readonly AutoResetEvent thread2Step = new AutoResetEvent(false);

    void DisplayThread1()
    {
        while (true)
        {
            Console.WriteLine("Display Thread 1");

            Thread.Sleep(1000);
            thread1Step.Set();
            thread2Step.WaitOne();
        }
    }

    void DisplayThread2()
    {
        while (true)
        {
            Console.WriteLine("Display Thread 2");
            Thread.Sleep(1000);
            thread2Step.Set();
            thread1Step.WaitOne();
        }
    }

    void CreateThreads()
    {
        // construct two threads for our demonstration;
        Thread thread1 = new Thread(new ThreadStart(DisplayThread1));
        Thread thread2 = new Thread(new ThreadStart(DisplayThread2));

        // start them
        thread1.Start();
        thread2.Start();
    }

    public static void Main()
    {
        MyThreadTest StartMultiThreads = new MyThreadTest();
        StartMultiThreads.CreateThreads();
    }
}

But this is not working. Th usage seem very straight-forward so I would appreciate if someone is able to show me what's wrong and where is the problem with the logic which i implement here.

Csch answered 23/1, 2013 at 14:7 Comment(9)
How is it not working? What happens?Reinhold
Well the code you've given won't even compile - you've never declared _stopThreads...Stan
@ Jon Skeet I just change the code to isolate the problem, now it's fixed.Csch
@Slaks I want to get synchronized calling but it is not.Csch
What do you expect to happen?Parshall
@Csch It works on my PC. What synchronisation you want to achieve between threads?Conversazione
Your code seems to ensure that both threads write a console message at roughly the same time. What do you want it to do?Jiminez
@Adrian Ciura I want to see Display Thread 1, Display Thread 2,Display Thread 1, Display Thread 2, and so on, without changing the order like Display Thread 1,Display Thread 1 or Display Thread 2, Display Thread 2.Csch
In other words I want to set the order of execution 1-2-1-2-1-2 and so on, not like now having 1-2-1-2-2-1-1-2...Csch
H
21

The question is not very clear but I'm guessing you're expecting it to display 1,2,1,2...

Then try this:

public class MyThreadTest
{
    static readonly AutoResetEvent thread1Step = new AutoResetEvent(false);
    static readonly AutoResetEvent thread2Step = new AutoResetEvent(true);

    void DisplayThread1()
    {
        while (true)
        {
            thread2Step.WaitOne(); 
            Console.WriteLine("Display Thread 1");
            Thread.Sleep(1000);
            thread1Step.Set();
        }
    }

    void DisplayThread2()
    {
        while (true)
        {
            thread1Step.WaitOne(); 
            Console.WriteLine("Display Thread 2");
            Thread.Sleep(1000);
            thread2Step.Set();
        }
    }

    void CreateThreads()
    {
        // construct two threads for our demonstration;
        Thread thread1 = new Thread(new ThreadStart(DisplayThread1));
        Thread thread2 = new Thread(new ThreadStart(DisplayThread2));

        // start them
        thread1.Start();
        thread2.Start();
    }

    public static void Main()
    {
        MyThreadTest StartMultiThreads = new MyThreadTest();
        StartMultiThreads.CreateThreads();
    }
}
Huarache answered 23/1, 2013 at 14:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.