Thread Safe
-
:) 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.
-
:) 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.
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
-
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
Hi Mav... Nice explanation..! Thanks M.Sendilkumar TVS Infotech Ltd Chennai
-
Hi Mav... Nice explanation..! Thanks M.Sendilkumar TVS Infotech Ltd Chennai
:) 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