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. .NET (Core and Framework)
  4. Thread Safe

Thread Safe

Scheduled Pinned Locked Moved .NET (Core and Framework)
questioncsharphelp
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.
  • S Offline
    S Offline
    Sendilkumar M
    wrote on last edited by
    #1

    :) Can any one help me to understand 'What is 'Thread Safe' in .Net? Almost all class explanation of MSDN help having this word 'Thread Safe'. Please help me to understand this.! ;) M.Sendilkumar TVS Infotech Ltd Chennai.

    M 1 Reply Last reply
    0
    • S Sendilkumar M

      :) Can any one help me to understand 'What is 'Thread Safe' in .Net? Almost all class explanation of MSDN help having this word 'Thread Safe'. Please help me to understand this.! ;) M.Sendilkumar TVS Infotech Ltd Chennai.

      M Offline
      M Offline
      mav northwind
      wrote on last edited by
      #2

      When you have more than one thread accessing the same object then problems can arise, mostly due to the scheduling performed by the OS. One thread can start modifying the object and before it's done another thread gets permission to run and will work on inconsistent data. In this respect, such an object would be not thread safe. Because implementing a thread safe object can be quite tricky and can include some overhead, not many classes in the framework are really thread-safe. For a not thread safe class, the developer has to take care that the same object cannot be used by two threads at the same time. Regards, mav

      S 1 Reply Last reply
      0
      • M mav northwind

        When you have more than one thread accessing the same object then problems can arise, mostly due to the scheduling performed by the OS. One thread can start modifying the object and before it's done another thread gets permission to run and will work on inconsistent data. In this respect, such an object would be not thread safe. Because implementing a thread safe object can be quite tricky and can include some overhead, not many classes in the framework are really thread-safe. For a not thread safe class, the developer has to take care that the same object cannot be used by two threads at the same time. Regards, mav

        S Offline
        S Offline
        Sendilkumar M
        wrote on last edited by
        #3

        Hi Mav... Nice explanation..! Thanks M.Sendilkumar TVS Infotech Ltd Chennai

        M 1 Reply Last reply
        0
        • S Sendilkumar M

          Hi Mav... Nice explanation..! Thanks M.Sendilkumar TVS Infotech Ltd Chennai

          M Offline
          M Offline
          mav northwind
          wrote on last edited by
          #4

          :) you're welcome. The problem with threads is that it's very easy to forget that they're there... Recently I've stumbled across a problem where a customer called and complained about an error message in on of my programs saying that an enumeration cannot be modified within a foreach loop. I was completely stumped because I had taken explicit care that I'm not removing items from a hashtable I was working with in a foreach, but still this error occurred. After a while it dawned on me: I had a timer that was checking the entries in the hashtable and this function worked flawless. But I also had a FileSystemWatcher set up that was adding entries to the hashtable. And when this occurred just when the timer callback was running this error occurred. :( Luckily, putting a lock(_myHashtable) { ... } here and there solved the problem quickly. mav

          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