对于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。

相关文章:

  • 2021-10-31
  • 2022-01-20
  • 2022-12-23
  • 2022-12-23
  • 2021-11-28
猜你喜欢
  • 2021-08-17
  • 2022-12-23
  • 2022-12-23
  • 2021-09-02
  • 2021-05-25
相关资源
相似解决方案