Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Define Thread Safe
Message
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Divers
Thread ID:
01367996
Message ID:
01368115
Vues:
8
>
>Which type of class you're talking about here? If it's a static class, then this is what is going to happen. Would this also happen with other types of classes?
>
>Can you please give a quick example?
>
>Thanks.

Yes, it can happen in any class. With a static class it may be more likely to happen since you're always using the same instance. But it's still possible with "normal" classes when you're using the same instance. Delegates are a big place where you can have multithreaded access calling the same instance of a class. Timers as well (since they're basically doing a delegate callback).

ex.
public class SampleThread
{
   public int Counter { get; set; }
   public void IncrementCounter()
   {
      for(int i = 0; I < 10; i++)
      {
         this.Counter++;
         Console.WriteLine(this.Counter);
      }
   }
}
Imagine a call coming in to IncrementCounter(). The counter is at 2 when another thread calls this same method on the same instance of the object. It increments the counter so it's now at 3. The first thread continues and prints 2 then loops and increments the counter again. It's now at 4; this thread skipped 3. This will continue as each thread runs. You may have even expected your counter to never go above 10, but in the multithreaded case it will go above this.
-Paul

RCS Solutions, Inc.
Blog
Twitter
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform