Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically verify some threading restrictions? (C#)

Our codebase has a lot of threading restrictions encoded in comments - such as:

  • This class is thread-safe (all public methods may be safely accessed from any thread)
  • Must hold a lock on "xyz" to access/invoke any public members
  • Must only be accessed from thread "xyz" (usually but not always referring to the GUI thread)
  • This lock must be taken after lock "xyz" if both are required

The first three can be seen on both classes and individual members.

I've searched for any solutions that could at least partially verify that these constraints are satisfied. I realise that there are major limitations on the extent to which this can be done automatically, but even a little help from a tool would really help. I couldn't find any though.

Can you suggest a tool to do something along these lines? Perhaps an FxCop ruleset that works by encoding the above restrictions as attributes?

like image 554
Roman Starkov Avatar asked Jan 25 '10 16:01

Roman Starkov


1 Answers

I am not aware of any thread-safety specific attributes, simply because it is generally too complex. And "testing" threaded code (with addition of extra Debug.Assert etc) is a common cause of heisenbugs. You could look at "CHESS"? It isn't a silver bullet, but it may be helpful.

like image 157
Marc Gravell Avatar answered Oct 22 '22 06:10

Marc Gravell