Can I add an attribute to a function to prevent reentry?

前端 未结 9 2330
梦谈多话
梦谈多话 2021-02-15 12:26

At the moment, I have some functions which look like this:

private bool inFunction1 = false;
public void function1()
{
    if (inFunction1) return;
    inFunctio         


        
9条回答
  •  -上瘾入骨i
    2021-02-15 12:56

    If this is for Thread safety you have to be careful with that variable.

    It's possible another thread could come into the function and pass the check before the first thread has set the variable.

    Make sure it's marked volatile like so:

    private volatile bool inFunction1 = false;
    

提交回复
热议问题