您的位置:首页 > Web前端

Thread-Safe的static data member初始化方法

2019-08-03 16:43 1121 查看
原文链接:https://www.geek-share.com/detail/2356332860.html 对于static data member的initialization,如果是在multi-threading的环境下,可以采用如下方法初始化:
 1              if (s_data == null)
 2              {
 3                  lock (s_dummy) // here s_dummy cannot be null
 4                  {
 5                      if (s_data == null)
 6                      {
 7                          // initialize s_data here
 8                      }
 9                  }
10              }
11 

这样做的好处是既不影响performance,又能确保thread-safe。

转载于:https://www.cnblogs.com/kevinwan/archive/2006/04/18/378027.html

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐