c# - Why does the Debug.Assert statement sometimes fail? -


i write below code, debug.assert raise fail. why debug.assert statement fail , how can fix it?

public class warehouse {    private int stockcount = 0;     public void decrementstock ()    {       if ( stockcount > 0 )          stockcount--;        debug.assert ( stockcount >= 0 )    }     public void incrementstock()    {       stockcount ++;    } } 

this smells multi-threading issue. suggest placing lock around access stockcount member.

public class warehouse {     private int stockcount = 0;     private object stocksynch = new object();     public void decrementstock ()    {        lock(stocksynch)        {           if ( stockcount > 0 )             stockcount--;            debug.assert ( stockcount >= 0 )        }     }      public void incrementstock()     {         lock(stocksynch)         {           stockcount ++;         }     }  } 

Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -