Listing E
// Solution 1
// (note: compiles with gcc 3.2, VC6)
#include "CriticalSection.h"
#include "message_handler_log.h"
 
 
// forward declaration
template< class char_type, class traits_type = std::char_traits< char_type> >
    class basic_thread_safe_log;
 
 
 
 
template< class char_type, class traits_type = std::char_traits< char_type> >
    class basic_internal_thread_safe_log
{
    typedef std::basic_ostream< char_type, traits_type> ostream_type;
    friend class basic_thread_safe_log< char_type, traits_type>;
 
 
    // non-copyiable
    typedef basic_internal_thread_safe_log< char_type, traits_type> this_class;
    basic_internal_thread_safe_log( const this_class &);
    this_class & operator=( this_class &);
 
 
public:
    basic_internal_thread_safe_log( ostream_type & underlyingLog)
        : m_underlyingLog( underlyingLog)
    {}
 
 
    void write_message( const std::basic_string< char_type, traits_type> & str)
    {
        CAutoLockUnlock locker( m_cs);
        m_underlyingLog << str;
    }
 
 
    void copy_state_to( ostream_type & dest) const
    {
        CAutoLockUnlock locker( m_cs);
        dest.copyfmt( m_underlyingLog);
        dest.setstate( m_underlyingLog.rdstate());
    }
 
 
    void copy_state_from( const ostream_type & src)
    {
        CAutoLockUnlock locker( m_cs);
       m_underlyingLog.copyfmt( src);
        m_underlyingLog.setstate( m_underlyingLog.rdstate());
    }
 
 
private:
    ostream_type & m_underlyingLog;
    mutable CCriticalSection m_cs;
};
 
 
typedef basic_internal_thread_safe_log< char> internal_thread_safe_log;
typedef basic_internal_thread_safe_log< wchar_t> winternal_thread_safe_log;
 
 
 
 
 
 
template< class char_type, class traits_type>
    class basic_thread_safe_log
    // *** protected, not public !!!
    : protected basic_message_handler_log< char_type, traits_type>
{
    typedef std::basic_ostream< char_type, traits_type> ostream_type;
    typedef basic_internal_thread_safe_log< char_type, traits_type> internal_type;
public:
    basic_thread_safe_log( internal_type & tsLog)
        : m_tsLog( tsLog)
    {
        // get underlying stream state
        tsLog.copy_state_to( ts() );
    }
  
    basic_thread_safe_log( const basic_thread_safe_log< char_type, traits_type> & from)
        : m_tsLog( from.m_tsLog),
          // ... on some platforms, a std::ostream base copy-constructor
          //    might be defined as private...
          basic_message_handler_log< char_type, traits_type>()
    {
        // get underlying stream state
        m_tsLog.copy_state_to( ts() );
    }
 
 
    ~basic_thread_safe_log()
    {
        // copy state to underlying stream
        m_tsLog.copy_state_from( ts() );
    }
 
 
    // get base class - to which we can write
    std::basic_ostream< char_type, traits_type> & ts()
    { return *this; }
 
 
protected:
    virtual void on_new_message( const string_type & str)
    { m_tsLog.write_message( str); }
 
 
private:
    internal_type  & m_tsLog;
};
 
 
 
 
typedef basic_thread_safe_log< char> thread_safe_log;
typedef basic_thread_safe_log< wchar_t> wthread_safe_log;
 
 
 
 
 
 
//////////////////////////////////////////////////////////
// Test
 
 
#include <iostream>
#include <fstream>
#include <iomanip>
 
 
const int THREADS_COUNT = 200;
const int WRITES_PER_THREAD = 500;
 
 
thread_safe_log get_log()
{
    static std::ofstream out( "out.txt");
    static internal_thread_safe_log log( out);
    return thread_safe_log( log);
}
 
 
LONG nRemainingThreads = THREADS_COUNT;
DWORD WINAPI WriteToLog( LPVOID lpData)
{
    int *pnThreadID = ( int *)lpData;
    // wait for all threads to be created, so that
    // we write at about the same time (stress it ;-))
    Sleep( 500);
 
 
    for ( int idx = 0; idx < WRITES_PER_THREAD; idx++)
    {
        get_log().ts() << "writing double: " << 5.23 << std::endl;
        get_log().ts() << "message " << idx << " from thread " << *pnThreadID << std::endl;
        // ... get other threads a chance to write
        Sleep( 1);
 
 
        if ( ( idx == 10) && ( *pnThreadID == 10))
        {
            // from now on, '5.23' will be written as '5,23'
            // (german locale)
            std::locale loc = std::locale( "german");
            get_log().ts().imbue( loc);
        }
  
    }
 
 
    InterlockedDecrement( &nRemainingThreads);
    delete pnThreadID;
    return 0;
}
 
 
int main(int argc, char* argv[])
{
    for ( int idx = 0; idx < THREADS_COUNT; ++idx)
    {
        DWORD dwThreadID;
        CreateThread( 0, 0, WriteToLog, new int( idx), 0, &dwThreadID);
    }
 
 
    // wait for all threads to end
    while ( true)
    {
        InterlockedIncrement( &nRemainingThreads);
        if ( InterlockedDecrement( &nRemainingThreads) == 0)
            break;
        Sleep( 100);
    }
    return 0;
}