tags:

views:

79

answers:

1

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)

+2  A: 

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().

steinar
Hey, thanks for the response. First just want to say, I know my Peek isn't actually a peek. It was originally, but then I changed it to a Dequeue to make sure it could Dequeue an object. I left the Console.WriteLine("(peek)") in there purely out of laziness when I changed it.
cost
Whoops, didn't realize hitting enter would post that. I'm not sure if your solution will work though. My initial version of the program didn't have the main loop stop after 10 iterations, it would use the goto to skip the i++ counter if the try failed. The program would put everything in the queue, then it would just run on and on, continuously trying to Dequeue the queue, but it would never work. Endless loop
cost
Hahah, look at this "Console.WriteLine("reading out {0}: {1}", i);" Something look wrong? I forgot to put in the second variable it's printing out, and I think that was somehow screwing it all up... Seems to work, or at least much better than it did before. Thanks for your help though, really appreciate it. I stuck a little Thread.Sleep in there and it keeps my processor usage from freaking out while looping.
cost