2

I currently use googles' gtest to write my unit tests, but it doesn't look like it can test thread-safety (that is, accessing something from multiple threads and ensuring it behaves according to spec).

What do you use to test thread safety? I'd like something cross-platform, but, it definitely has to work on Windows atleast.

Thank you!

  • 6
    I'm no MT expert, but I don't think you can automate MT design testing, the whole thing is rather non-deterministic. – Cat Plus Plus Jun 20 '11 at 17:34
  • It should be possible to check certain "contracts", though. – Arthur Hopkin Jun 20 '11 at 17:38
  • 2
    possible duplicate of [Testing approach for multi-threaded software](http://stackoverflow.com/questions/2437763/testing-approach-for-multi-threaded-software) – Mark B Jun 20 '11 at 17:45
  • Note that testing MT software is a really hard problem in general. Also see http://stackoverflow.com/questions/2059895/are-there-any-automated-unit-testing-frameworks-for-testing-an-in-house-threading and http://stackoverflow.com/questions/2035890/unit-testing-concurrent-code – Mark B Jun 20 '11 at 17:46

1 Answers1

1

If multithreaded code isn't immediately, obviously, provably correct then it is almost certainly wrong. And if it is, you don't need to test it.

Seriously: shared mutable state should be extremely localised and rare, and the classes that do it should be demonstrably correct.

Your threads should normally interact via safe primitives (eg a thread-safe work queue). If you have lots of data structures scattered around your code each with its own locking strategy then your code almost certainly contains deadlocks and race conditions. A big testing effort will only find some of the problems.

Alan Stokes
  • 18,815
  • 3
  • 45
  • 64