I have been writing a C++ method entry/exit logger, based on RAII. The usage is something like this:
void Class::Method()
{
METHOD_NAME( "Class::Method" ); // I know: I could use __FUNCTION__ instead ;<)
…
}
Logger class:
#define METHOD_NAME(name) TraceLogger _traceLog(name);
TraceLogger::TraceLogger( const std::string& theContext )
{
<lock mutex here>
// Trace logging code here
}
TraceLogger::~TraceLogger()
{
<lock mutex here>
// Trace logging code here
}
The problem is that the code is not thread safe. If I add a mutex here to protect the code, will it be correctly used, since it would be constructed and then immediately used in the TraceLogger constructor?
We are a windows VS2008 shop (that is, there is no std::mutex available), and are using home grown thread locking code. I have tried using this, and it doesn't seem to work.
Are there any standard ways to do to this?
Thanks, Paul