multithreading - C# : What if a static method is called from multiple threads? -


I have a static method in my application that is called multiple threads at the same time. Is there any danger of my data mixing?

In my first attempt, this method was not static and I was making many examples of classes. In that situation, my data was found in some way. I'm not sure how this happens because it happens only sometimes I am still debugging but now the method is stable but I have no problem till now. Maybe it's just luck I certainly do not know. Variable declared within the method (" Captured " variables) separated by

So you will not get any inherent problem;

The examples of share-state will be:

  • Static fields
  • However, items derived from a normal cache (non-serialized)
  • The data obtained through the input parameters (and the state on those objects), if possible, multiple threads are touching the same object (s)

If you have shared the state , Then you should either:

  • Once it can be shared, once the state does not change it Khana (Better: Use irreversible items to represent the state, and take a snapshot of the state in a local variable - that is, the reference . Any data. Frequently, > Anything that reads in the data Once , use the local variable, and then use the variable - note that it only helps in the irreversible state!)
  • < Li> Synchronize access to data (synchronize all threads Is Must) - either mutually exclusive or (more granular) reader / writer

Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -