Domanda

I am currently working on a large project which has recently had many changes, most prominently the addition of threading support.

While going through the code I have identified sections that may potentially cause race conditions, if not now then sometime in the future. In order to prevent such a regression, I would like to write a test that can reliably detect a race condition in that particular region so as to ensure that no future commits cause this fault.
The code isn't littered with sleep() statements, but is a potential deadlock and race minefield and I want to ensure robustness.

This project is written completely in C. So, is there anyway for me to write unit tests to prevent race conditions?

È stato utile?

Soluzione

Race conditions are inherently a result of non-determinism. If you cannot ensure that the calling sequence is secure, then introduce a number of run-time checks that verify the protocol invariants are honoured. Then, at least you will have evidence of a fault whenever they occur.

While this won't solve your problem, it at least gives you a tool to quantify the extent of the problem.

If any of the races are triggered from events outside the scope of the application, then any static analysis would require this to also be modeled to be able to detect the conditions.

Altri suggerimenti

Valgrind DRD tool can be used to detect many thread errors. Just use this tool and run your usual test cases.

Clang Thread Sanitizer works by watching your test process as it executes. Whenever a thread reads or writes memory unguarded, it records it, and it will tell you if any piece of memory was accessed unguarded by more than one thread.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top