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
Post a Comment