I read a GitHub project description about using template metaprogramming to auto-detect race conditions |
Can you link the project?
A meta-program is a program whose output is source code. In this case the meta-program is written in C++ and outputs C++, but the language in which the meta-program is written doesn't really matter.
There are at least two options:
a.) monitor the executing program and wait for a race to occur;
b.) analyze a controlled subset of the program's source code and prove that no races occur.
Option a is conceptually easy: it involves adding code into the system that detects race conditions. This is typically done with the C++ compiler's help (e.g., with Clang's ThreadSanitizer module), but at least in principle any capable metaprogram could do it too. The compiler (and ThreadSanitizer) is just a metaprogram that eats your source code and outputs it in another form.
Option b is conceptually harder: there are some rules which, if obeyed, guarantee the absence of race conditions. A metaprogram could be constructed to implement a language that makes race conditions impossible by definition.
For example,
- Robert Ramey's Safe Numerics is a template library which implements a compiler for a language which does arithmetic safely. If arithmetic is expressed using the library's language, it is guaranteed to either be free of integer overflow or to result in a compiler error.
- Hana Dusikova's compile time regular expression library (CTRE) is a template library which implements a compiler for the language of regular expressions. If a regular expression is written in the library's language, it is guaranteed to compile into a valid regular expression matcher, or to result in a compiler error.
- Boost Spirit is a (much older) template library which implements a parser generator - a compiler for the language of attribute grammars. If a grammar is written in the library's language, it is guaranteed to either compile to a working parser or to result in a compiler error.
Actually in the case of Spirit or CTRE, correctness isn't the primary focus, so there may be certain invalid sentences that type-check, but the nature of the implementation will probably disallow most of them. Ramey's library is the best example, it is designed specifically to prove the absence of a particular kind of error, exactly what the question is looking for. It is also the simplest internally.