Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

双if加锁的方式是否是更合理 #1

Open
Emrys5 opened this issue Jun 24, 2018 · 3 comments
Open

双if加锁的方式是否是更合理 #1

Emrys5 opened this issue Jun 24, 2018 · 3 comments

Comments

@Emrys5
Copy link

Emrys5 commented Jun 24, 2018

  if (policy == null)
            {
                lock (this)//因为Invoke可能是并发调用,因此要确保policy赋值的线程安全
                {
                    if (policy == null)
                    {
                           ...
                     }
                  }
             }
@xujiesh0510
Copy link

嗯,闻到了坏代码的味道,来看了下,果然有哥们已经指出来了

@yangzhongke
Copy link
Owner

谢谢。lock (policies)外面已经加锁了

@SuperSnowYao
Copy link

  if (policy == null)
            {
                lock (this)//因为Invoke可能是并发调用,因此要确保policy赋值的线程安全
                {
                    if (policy == null)
                    {
                           ...
                     }
                  }
             }

已经lock(policies)了,就用不着加双if了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants