I have seen tools which can run a multithreaded program deterministically, even in the presence of race conditions. Now I wonder if there is any tool which can actually detect races and rewrite the code (at runtime) to not have the detected races in the future.
Does such kind of a tool exists? Or is it too hard to create one? I think it can be created with a tool that does dynamic binary translation of code, such as PIN or valgrind.
While running a program in a deterministic manner is too much to ask, detecting some (if not all) race conditions is not difficult.
A lot of research is presently happening in this area. And you are on the right track with
PIN -------> Intel Parallel Studio Valgrind --> Cachegrind and ThreadSanitizer 1.0
There are many open source and commercial tools. I used to work on 1
The way such tools work is that they track lock and other information per memory access and maintain a history of previous accesses. Every access is compared with history of previous accesses. There is more to it.