CountDownEvent won't wait until all the signals have been called

6.8k views Asked by At

I am looking at this site for threads. I have been playing with the code to answer the question "Does the CountdownEvent stop all threads?" the answer I got was no. Then I decided to play with the number that is passed into the CountdownEvent. Here is my code

namespace ThreadPractice
{
    class Program
    {
        static CountdownEvent CountDown = new CountdownEvent(4);
        static void Main()
        {
            new Thread(() => SaySomething("I am Thread one.")).Start();
            new Thread(() => SaySomething("I am thread two.")).Start();
            new Thread(() => SaySomethingElse("Hello From a different Thread")).Start();
            new Thread(() => SaySomething("I am Thread Three.")).Start();
            CountDown.Wait();
            Console.Read();
        }

        static void SaySomething(string Something)
        {
            Thread.Sleep(1000);
            Console.WriteLine(Something);
            CountDown.Signal();
        }

        static void SaySomethingElse(string SomethingElse)
        {
            Thread.Sleep(1000);
            Console.WriteLine(SomethingElse);
        }
    }
}

I am expecting that the thread that calls SaySomethingELse() to execute but the other threads execute as well even though only four signals have been called.

Why does it do that?

Thanks,

dhoehna

2

There are 2 answers

1
Jon Skeet On BEST ANSWER

It looks to me like you've got Signal and Wait the wrong way round. If you want the SaySomething calls to wait for the countdown to reach 0, you should be calling Wait. Here's an example:

using System;
using System.Threading;

namespace ThreadPractice
{
    class Program
    {
        static CountdownEvent CountDown = new CountdownEvent(4);
        static void Main()
        {
            new Thread(() => SaySomething("I am Thread one.")).Start();
            new Thread(() => SaySomething("I am thread two.")).Start();
            new Thread(() => SaySomethingElse("Hello From a different Thread")).Start();
            new Thread(() => SaySomething("I am Thread Three.")).Start();
            for (int i = 0; i < 4; i++)
            {
                Console.WriteLine("Calling Signal (time #{0})", i);
                CountDown.Signal();
                Thread.Sleep(1000);
            }
            Console.WriteLine("Done"); 
        }

        static void SaySomething(string Something)
        {
            CountDown.Wait();
            Console.WriteLine(Something);
        }

        static void SaySomethingElse(string SomethingElse)
        {
            Thread.Sleep(1000);
            Console.WriteLine(SomethingElse);
        }
    }
}

Output:

Calling Signal (time #0)
Hello From a different Thread
Calling Signal (time #1)
Calling Signal (time #2)
Calling Signal (time #3)
I am Thread one.
I am Thread Three.
I am thread two.
Done
2
Idle_Mind On

Hmmm...it looks to me like you want to wait for all the threads to finish before continuing on in the main thread. If so, you forgot to Signal() in SaySomethingElse(). This prevents CountDown.CurrentCount from reaching 0 (zero) and is why your main thread is "stuck" You set it to 4 and it only goes down to 1 (one). Fix it and you should get the desired? results:

class Program
{
    static CountdownEvent CountDown = new CountdownEvent(4);
    static void Main()
    {
        new Thread(() => SaySomething("I am Thread one.")).Start();
        new Thread(() => SaySomething("I am thread two.")).Start();
        new Thread(() => SaySomethingElse("Hello From a different Thread")).Start();
        new Thread(() => SaySomething("I am Thread Three.")).Start();

        CountDown.Wait();

        Console.WriteLine("Done!");
        Console.Read();
    }

    static void SaySomething(string Something)
    {
        Thread.Sleep(1000);
        Console.WriteLine(Something);
        CountDown.Signal();
    }

    static void SaySomethingElse(string SomethingElse)
    {
        Thread.Sleep(1000);
        Console.WriteLine(SomethingElse);
        CountDown.Signal();
    }
}

Output:

I am Thread one.
I am thread two.
Hello From a different Thread
I am Thread Three.
Done!