samedi 28 février 2015

How to avoid violating one definition rule in this case?


We have an existing Mutex C++ class. It has a debug feature to track the owner thread id. So it has a member variable to store this, but because the original developer doesn't want to waste memory, this member is wrapped by a macro in a header file.


This header file has been included in multiple projects, some binaries are developed by other departments, so for those libraries we cannot control the compile flag switch, and we do not want this strong coupling.



// mutex.h
class Mutex
{
......
private:
#ifdef _DEBUG
int owner_thread_id_;
#end
......
}


Recently we were bitten by this code, because of memory corruption caused by violating the one definition rule.


So how can we implement it with following two requirements:



  1. Don't waste memory because it is a utility class and wide used.

  2. Don't violating the one definition rule.





Aucun commentaire:

Enregistrer un commentaire