c# - Should I unit test for multithreading problems before writing any lock? -


I am writing a class that I know needs a lock, because the class must be thread safe. But as I'm tested-driven, I know I can not write a line of code before making a test for it. And I find it very difficult to do because the test will be very complex at the end. What do you usually do in those cases?

Public class StackQueu {Private Stack & LT; WebRequestInfo & gt; Stack = new stack & lt; WebRequestInfo & gt; (); Private queue & lt; WebRequestInfo & gt; Line = new line & lt; WebRequestInfo & gt; (); Public Int Calculation {Receive this return. Statute + this.stack.Count; }} Public Zero Enquery (WebRequestInfo requestInfo) {this.queue.Enqueue (requestInfo); } Public Zero Push (WebRequestInfo requestInfo) {this.stack.Push (requestInfo); } Private WebRequestInfo Next () {If (stack.Count> gt; 0) {return stack.Pop (); } And if (queue.count> gt; {return queue.qq;}; } Return tap; }}

When writing multi-threaded code, you should use your brain to normalize . You should make a rational argument about every single line of code, whether the thread is safe or not, it is like proving the correctness of a mathematical formula - you are just giving examples of the values ​​of N, all for "N + 1> N N "with which the formula is correct. Similarly, it is to prove that writing a test case is not possible with a class thread-safe that tries to expose problems with it. It is possible to prove with a test that there is a mistake in it, but not that there is no flaw.

The best part is that you can reduce the need for multi-threaded code. Preferably there should be no multi-threaded code in the application (for example, depending on the thread-safe library and the appropriate design pattern), or it should be restricted to a very small area. Your StackQueue class looks quite easy, so that you can secure it safely with little thought.

Assume that stack and queue are deployed thread-safe (I do not know. NET's libraries), you just click next () need to be thread-protected calculation is already thread-safe, because a client uses securely returned values ​​without using client-based locking Otherwise the code will be broken.

Next () is not thread-safe, because there are state dependencies between methods if threads T1 and T2 calls stack.Count at the same time And it gives 1, then one of them will get the value with stack.opop () , but when the other will call , the stack will be empty ( Which then starts throwing InvalidOperationException ). You will need a stack and queue with non-blocked versions of pop () and Dequeue () (they are empty which are blank). Then the code thread will be protected when it is written:

  Private WebRequestInfo next () {WebRequestInfo next = stack.PopOrNull () if (next == zero) {next = queue.DequeueOrNull () ; } Next Return; }  

Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -