Hi,
I've written a small logging class which I use for debugging in a multithreaded environment. I.e. different concurrent (boost) threads may instantiate an object of class logmsg, which amongst other things writes some message to the console. To synchronize the output I lock a global mutex whenever the logmsg constructor is invoked. Unfortunately it simply doesn't work. I still end up with output from different threads being mixed up almost as if there was no synchronization. I write 'almost' because if I remove the locking it's actually getting worse (crazy, huh). I added the code for allocating the console (it's an MFC project), because I believe it could have something to do with it, since the console itself is running in a separate (non-boost) thread.
---
FILE* pConsole;
boost::mutex consoleMutex;

class logmsg
{
public:
  logmsg(const void* source, const char* msg, ...);
};
logmsg::logmsg(const void* source, const char* msg, ...)
{
  boost::mutex::scoped_lock lock(consoleMutex);

  if (!pConsole)
    create_console();
  cout << msg << endl;
}
void create_console()
{
  AllocConsole();
  int hConsole = _open_osfhandle(
  (long)GetStdHandle(STD_OUTPUT_HANDLE),
  _O_TEXT
  );
  pConsole = _fdopen(hConsole, "w"); 
  setvbuf(pConsole, NULL, _IONBF, 0); //disables buffering
  *stdout = *pConsole; 
}

---

Best regards,
Alex