2

Is there any automated tool to check C# .NET code for thread safety?

p.s. It doesn't have to be perfect, but it should check for the obvious things that can be checked at compile time.

Contango
  • 76,540
  • 58
  • 260
  • 305
  • 1
    Define "thread safety". And I really doubt a tool will be useful for that. – driis Sep 28 '11 at 17:15
  • 3
    Almost certainly a duplicate question, but: CHESS http://research.microsoft.com/en-us/downloads/b23f8dc3-bb73-498f-bd85-1de121672e69/ – Marc Gravell Sep 28 '11 at 17:19
  • http://stackoverflow.com/questions/955561/how-to-test-for-thread-safety ? – Marc Gravell Sep 28 '11 at 17:20
  • Scratch "at compile time" off your list of requirements, that magic doesn't exist. – Hans Passant Sep 28 '11 at 17:47
  • 2
    Can you name one of the "obvious things that can be checked at compile time" and what relationship they have to "thread safety"? For example: it would be straightforward to write a tool that detects usages of "lock(this)" at compile time. Locking "this" is a *bad programming practice* but that does not necessarily make it *thread-unsafe*. What's an example of something that is both "unsafe" and detectable at compile time? – Eric Lippert Sep 28 '11 at 18:00
  • Something it might check is modifying a shared collection within two different public methods that is known to be unsafe. Seems like an obvious thread-safety problem. Another example of a problem it could detect is reading and modifying a shared 64-bit value within two different internal or public methods of a class. There's numerous ways to accidentally write unsafe code. The tool could perhaps be told which methods are allowed to be executed from multiple threads and then conduct its static analysis accordingly. – Lee Grissom May 01 '13 at 02:21

1 Answers1

1

The only thing I've seen is TypeMock's racer. It isn't compile time, but it can make some thread bugs (like deadlocking) possible to test.

Lou Franco
  • 87,846
  • 14
  • 132
  • 192