码迷,mamicode.com
首页 > 其他好文 > 详细

Thread in depth 3:Synchronization

时间:2017-04-28 10:26:43      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:rman   his   mem   corrupt   oca   read   ram   struct   mis   

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:

  1. 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.
  2. 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

标签:rman   his   mem   corrupt   oca   read   ram   struct   mis   

原文地址:http://www.cnblogs.com/lwhkdash/p/6778361.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!