Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to use Queue.Synchronized

How to use Queue.Synchronized

Scheduled Pinned Locked Moved C#
questiondata-structureshelptutorialannouncement
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JoeRip
    wrote on last edited by
    #1

    It's not clear to me how I am expected to use Queue Synchronization. Is this a general state, which makes the Queue thread safe? Or is this a short, temporary measure I use to lock the queue before I read or write it, and immediately release it when I'm done? The documentation and samples are too short and don't go into detail on this. So my question is: do I simply wrap my Queue with a synchronized wrapper and leave it on, doing all further access through that wrapper? Or do I only wrap the Queue when I'm accessing it, and immediately drop the wrapper when I'm done? Do I pass the wrapper to other objects, or only the original Queue? Thanks for any help here.

    M 1 Reply Last reply
    0
    • J JoeRip

      It's not clear to me how I am expected to use Queue Synchronization. Is this a general state, which makes the Queue thread safe? Or is this a short, temporary measure I use to lock the queue before I read or write it, and immediately release it when I'm done? The documentation and samples are too short and don't go into detail on this. So my question is: do I simply wrap my Queue with a synchronized wrapper and leave it on, doing all further access through that wrapper? Or do I only wrap the Queue when I'm accessing it, and immediately drop the wrapper when I'm done? Do I pass the wrapper to other objects, or only the original Queue? Thanks for any help here.

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      Once you call Synchronized(), you should use the returned wrapper everywhere you would have used the original unsynchronized queue. Use of the queue through the wrapper will be thread safe for all "single operation" methods on the queue. Make sure Synchronized() is really what you need for a thread safe queue. Enumerating and indexing the queue aren't going to be thread safe so you may need additional locks. Making your own thread safe wrapper (or derived) queue class can be better in some situations. It allows you to implement locks the way you need to (like using a ReaderWriterLockSlim for producer/consumer thread access) and provide properly locked enumerating methods. Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      J 1 Reply Last reply
      0
      • M Mark Salsbery

        Once you call Synchronized(), you should use the returned wrapper everywhere you would have used the original unsynchronized queue. Use of the queue through the wrapper will be thread safe for all "single operation" methods on the queue. Make sure Synchronized() is really what you need for a thread safe queue. Enumerating and indexing the queue aren't going to be thread safe so you may need additional locks. Making your own thread safe wrapper (or derived) queue class can be better in some situations. It allows you to implement locks the way you need to (like using a ReaderWriterLockSlim for producer/consumer thread access) and provide properly locked enumerating methods. Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        J Offline
        J Offline
        JoeRip
        wrote on last edited by
        #3

        Thanks for the response. Okay, so the Sync wrapper persistently replaces the Q itself, and it is the object I should pass around between methods, etc. That was not entirely clear to me before. This should probably work for what I'm doing; I spawn a listener object on a new thread, which listens for events from a COM object. My main thread passes various Queues to the spawned object (actually, sets them as properties), and the spawned object, when it receives messages, puts a notation in the queue(s). My main thread polls the queue, dequeuing any notations it finds. So there's no enumerating or indexing, I think. Of course, I haven't written it yet... :-)

        M 1 Reply Last reply
        0
        • J JoeRip

          Thanks for the response. Okay, so the Sync wrapper persistently replaces the Q itself, and it is the object I should pass around between methods, etc. That was not entirely clear to me before. This should probably work for what I'm doing; I spawn a listener object on a new thread, which listens for events from a COM object. My main thread passes various Queues to the spawned object (actually, sets them as properties), and the spawned object, when it receives messages, puts a notation in the queue(s). My main thread polls the queue, dequeuing any notations it finds. So there's no enumerating or indexing, I think. Of course, I haven't written it yet... :-)

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          Multiple writer threads and a single reader thread, right? The Synchronized() queue should indeed be fine for that. Cheers, Mark

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups