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. Synchronized collections

Synchronized collections

Scheduled Pinned Locked Moved C#
csharphelpquestion
4 Posts 3 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.
  • D Offline
    D Offline
    Don Miguel
    wrote on last edited by
    #1

    Hello, Please someone explain to me which is the need of Sychronized and SyncRoot in .NET collections. Why and how should I use this two features. Aren't the collections thread safe?? And if them aren't, a lock is not enough? Really, I think I have a big missunderstood about collections, please help.:(( Thanks, Miguel

    K J 2 Replies Last reply
    0
    • D Don Miguel

      Hello, Please someone explain to me which is the need of Sychronized and SyncRoot in .NET collections. Why and how should I use this two features. Aren't the collections thread safe?? And if them aren't, a lock is not enough? Really, I think I have a big missunderstood about collections, please help.:(( Thanks, Miguel

      K Offline
      K Offline
      Kannan Kalyanaraman
      wrote on last edited by
      #2

      I'm not expert here, but here is my understanding. I believe most of the collections have the SyncRoot element, and when you are locking the collections you need to lock the SyncRoot element. So if you are locking a hastable, it should look something like lock(myhashtable.SyncRoot). The locking keyword in c# is a bit misleading, actually what it does is grant you access to the lock, so that no other thread can acquire the lock until you release it. regards Kannan

      1 Reply Last reply
      0
      • D Don Miguel

        Hello, Please someone explain to me which is the need of Sychronized and SyncRoot in .NET collections. Why and how should I use this two features. Aren't the collections thread safe?? And if them aren't, a lock is not enough? Really, I think I have a big missunderstood about collections, please help.:(( Thanks, Miguel

        J Offline
        J Offline
        James T Johnson
        wrote on last edited by
        #3

        Don Miguel wrote: which is the need of Sychronized Synchronized tells you if the collection's operations will be thread-safe. Generally this is done by implementing a wrapper around the collection, using a lock on the SyncRoot for each operation performed. Don Miguel wrote: which is the need of SyncRoot SyncRoot is for doing your own locking. Say you need to add a lot of items to the collection, during which no other thread should use it. Rather than using the Synchronized wrapper which would lock/unlock for each operation you would create a lock on the SyncRoot object, then perform each of your operations. Don Miguel wrote: Aren't the collections thread safe?? No, you need to refer to the documentation for each class you wish to use to find out if the operations are thread-safe. Making everything thread-safe by default is expense CPU wise, so you are better off not putting it in for the cases where thread-safety isn't an issue. Most collection classes expose some way of getting a Synchronized version, usually by a static method of some sort. Always check the documentation to be sure. James "It is self repeating, of unknown pattern" Data - Star Trek: The Next Generation

        D 1 Reply Last reply
        0
        • J James T Johnson

          Don Miguel wrote: which is the need of Sychronized Synchronized tells you if the collection's operations will be thread-safe. Generally this is done by implementing a wrapper around the collection, using a lock on the SyncRoot for each operation performed. Don Miguel wrote: which is the need of SyncRoot SyncRoot is for doing your own locking. Say you need to add a lot of items to the collection, during which no other thread should use it. Rather than using the Synchronized wrapper which would lock/unlock for each operation you would create a lock on the SyncRoot object, then perform each of your operations. Don Miguel wrote: Aren't the collections thread safe?? No, you need to refer to the documentation for each class you wish to use to find out if the operations are thread-safe. Making everything thread-safe by default is expense CPU wise, so you are better off not putting it in for the cases where thread-safety isn't an issue. Most collection classes expose some way of getting a Synchronized version, usually by a static method of some sort. Always check the documentation to be sure. James "It is self repeating, of unknown pattern" Data - Star Trek: The Next Generation

          D Offline
          D Offline
          Don Miguel
          wrote on last edited by
          #4

          Many thanks James! You was very explicit, it helps me much. Don Migule

          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