cost
cost

Reputation: 4480

My Queue is acting funny, any idea why?

I wanted to do some testing with queues and using Queue.Synchronized to see how it worked out when having two threads putting stuff into a queue, and a third thread reading it out. It seemed fairly simple, but it's not working right and I'm not sure why. Probably a silly mistake on my part. There's a bit more to it though.

class Program
{
    public int this1 = 0;
    static void Main(string[] args)
    {
        Tester test1 = new Tester();

        Queue myQ = new Queue();
        Tester.myQ = Queue.Synchronized(myQ);
        Thread test1_thread = new Thread(new ThreadStart(test1.test1));
        Thread test1_thread2 = new Thread(new ThreadStart(test1.test1));

        test1_thread.Start();
        test1_thread2.Start();

        int i = 0;
        while (i <= 10)
        {

            i++;
        go_back_here:
            try
            {
                Tester.myQ.Enqueue(40);
                Console.WriteLine("did it");
                int holding = Convert.ToInt32(Tester.myQ.Dequeue());
                Console.WriteLine("reading out {0}: {1}", i);
            }
            catch
            {
                //Console.Write("e");
                //goto go_back_here;
            }
        }
        Console.ReadLine();
    }


}

class Tester
{

    public static Queue myQ;
    public void test1()
    {
        int this1 = 0;

        while (this1 <= 10)
        {
            Console.WriteLine("Doing something {0}", this1);
            myQ.Enqueue(this1);
            Console.WriteLine("(peek) {0}", myQ.Dequeue());
            this1++;
        }

    }
}

Through my own testing I've found that the two Tester threads are loading stuff into the queue fine. When my main loop would try to Dequeue it, I would get an error saying the queue was empty. I put peeks and then Dequeues in the Testing threads and they were able to show the stuff in the thread fine.

Then I wondered if I was somehow accessing the wrong Queue from the main loop (OOP is not my forte, still learning it), so I added "Tester.myQ.Enqueue(40);" to see if I could stick something in it. My peeks/Dequeues didn't show it, but, it DID show up when I added the "did it" write line. I've run the program a ton of times and the 40 from the main loop only appears in the Testing threads' printing out of Dequeue when the "did it" is there. Never when I comment it out.

Could someone more knowledgeable about the subject shed some light on what's going on? (and yes, I know my use of a goto is a terrible thing, I let it slide because this was supposed to be a simple test)

Upvotes: 1

Views: 266

Answers (1)

steinar
steinar

Reputation: 9653

It seems that your initial problem was that your main thread was accessing the queue too soon (nothing in it because the threads where starting to put something in it at that same time). You will need to ensure that the threads have finished working before reading from the queue in the main thread. You can wait for them by calling Thread.Join or, alternatively, Thread.Sleeping in a loop while checking for the condition of some global variable set by the threads. It's not enough to have a while loop without sleep or join.

Furthermore, your "peek" isn't really a peek because it actually removes the item. You need to use Console.WriteLine("(peek) {0}", myQ.Peek()); rather than myQ.Dequeue().

Upvotes: 2

Related Questions