Thread in depth 3:Synchronization
Posted 灰色世界
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Thread in depth 3:Synchronization相关的知识,希望对你有一定的参考价值。
Synchronization means multi threads access the same resource (data, variable ,etc) should not cause a corruption to it.If all method of a class promise threading synchronization,we call that the class is "Thread Safety".
ALL static methods of the .net framework are promised thread-safe. When we are writting class and method we should pay a little bit attention on the thread safety,we can:
- Avoid to use static data.Different thread can access the same static data at the same time so may cause corruption to it. Use instance data or value-type data. value-type data will always be copied to the stack of the thread,so does the local variable of a instance data.
- Use lock construct. But this will cause a performance problem,because the thread will be blocked or being "spin-locked" and waste CPU and memory, so use it very carefully.
以上是关于Thread in depth 3:Synchronization的主要内容,如果未能解决你的问题,请参考以下文章