Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test for thread safety [duplicate]

Do you have any advices how to test a multithreaded application?

I know, threading errors are very difficult to catch and they may occur at anytime - or not at all. Tests are difficult and the results are never for sure. Certainly it is best to carefully design and program the concurrent modules.
Nevertheless - I do not want to leave out the test aspect. So running a lot of threads that are all working on the same items can invoke threading errors, sometimes.

Any ideas or best practices to get a high hit rate of hidden threading errors?
(I am using .Net/C#)

like image 740
tanascius Avatar asked Jun 05 '09 12:06

tanascius


People also ask

How can you tell if something is thread-safe?

To test if the combination of two methods, a and b, is thread-safe, call them from two different threads. Put the complete test in a while loop iterating over all thread interleavings with the help from the class AllInterleavings from vmlens. Test if the result is either an after b or b after a.

How is the safety of a thread achieved?

Using Atomic Variable Using an atomic variable is another way to achieve thread-safety in java. When variables are shared by multiple threads, the atomic variable ensures that threads don't crash into each other.

What is multithreaded testing?

Multithread testing: Multithreaded testing is where concurrent transactions of an application are running at the same time. It can run on a single machine or distribute across multiple machines.


2 Answers

You can use some good tools to test all the threading issues like Data races, deadlocks, stalled threads etc. intel-thread-checker is one such good tool.

You can also try, CHESS by Microsoft Research

like image 145
aJ. Avatar answered Sep 19 '22 12:09

aJ.


Try increasing the number of threads to a large number if possible, even beyond how many will be used in a release. With lots of threads running your program, an error will appear more often since more threads are running over the code.

Double check your declarations, locks, unlocks, semaphore counts, etc and make sure they make sense.

Create a test document or spreadsheet, and using your knowledge of the code, think about where possible race conditions or deadlocks could occur.

Grab some people from the hall and do a 'hallway usability test' (Joel on Software said that I think?). Generally, people who have no idea what your program does/is about will be able to break it easily.

like image 34
samoz Avatar answered Sep 21 '22 12:09

samoz