Skip to main content
GameDev.net gamedev.net
🔒 Locked

Syncronized Typed Queues

Started by WadeyBoi Sep 23, 2009 at 7:44 AM 2 replies 1.4k views
Original Post
WadeyBoi
WadeyBoi
Hi all, I have been working on the following C# programming issue for over a week now and don't seem to be getting anyway. So was wondering if anyone would be able to help. I have created a struct called Message I then have two queues of typed Message one normal and the other syncronized which will be used to store the Message structures. However, C# doesn't like Queue.Synchronized for my Message queue. However if I use a string queue then it works perfectly.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Collections;

namespace ThreadPractice
{
    class Control
    {
        public struct Message
        {
            public string name;
            public string body;

            public Message(string messageName,string messageBody)
            {
                name = messageName;
                body = messageBody;
            }
        };

        private Queue<Message> m_ReceivedQueue;
        private Queue<Message> m_ReceivedQueueSyncd;
        private Message m_textmessage;

        //----------------------------------------------------------------------

        public Control(UInt16 rtNumber)
        {
            // Instantiate Message Object and Queue
            
            m_ReceivedQueue = new Queue<Message>();
            m_ReceivedQueueSyncd = Queue.Synchronized(m_ReceivedQueue);
            Message m_textmessage = new Message();
            m_textmessage.name = "test";
        }

        //----------------------------------------------------------------------
        // Get Received Message Queue
        //----------------------------------------------------------------------

        public Queue GetMessageQueue()
        {
            lock (this)
            {
                return m_ReceivedQueueSyncd;
            }
        }

        public Message getTextMessage()
        {
            return m_textmessage;
        }
    }

}


Anybody got any ideas how to synchronize my Message queue. Looking forward to your replies Cheers Steve [Edited by - WadeyBoi on September 23, 2009 10:51:31 AM]
felisandria
felisandria
I don't think System.Collections.Generic.Queue is inherently thread-safe. Is it possible you're getting interference from that? I would imagine that it takes "longer" to process a template than a string, which might explain the discrepancy.

ETA: Although I suppose I really should have asked what exactly it's telling you, first. What's the error or behavior?
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~
WadeyBoi
WadeyBoi
Quote:
Original post by felisandria
I don't think System.Collections.Generic.Queue is inherently thread-safe. Is it possible you're getting interference from that? I would imagine that it takes "longer" to process a template than a string, which might explain the discrepancy.

ETA: Although I suppose I really should have asked what exactly it's telling you, first. What's the error or behavior?

The errors I receive are below:

Error	1	The best overloaded method match for 'System.Collections.Queue.Synchronized(System.Collections.Queue)' has some invalid arguments	F:\Work\Final Year\DISSERTATION\CODE\XP Code\ThreadPractice\ThreadPractice\Control.cs	35	36	ThreadPracticeError	2	Argument '1': cannot convert from 'System.Collections.Generic.Queue<ThreadPractice.Control.Message>' to 'System.Collections.Queue'	F:\Work\Final Year\DISSERTATION\CODE\XP Code\ThreadPractice\ThreadPractice\Control.cs	35	55	ThreadPractice
felisandria
felisandria
Oh, right. That's because things like Synchronized aren't actually implemented in the generic version of Queue. It's been auto-converting the string to a non-generic to use the non-generic's synchronize capability, but it has no idea how to do that for your "message" type.

Why not just lock it yourself?
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.