Re: Mutex in ostream operator - doesn't work
On Jan 29, 5:08 am, "Christopher Pisz" <some...@somewhere.net> wrote:
"Hansel Stroem" <han...@stroem.com> wrote in message
news:479e9bc6$0$25047$607ed4bc@cv.net...
[...]
What a coincidence that I am studying a very simular problem
in a book as we speak. The authors of "Standard C++ IoStreams
and Locals" suggests that you add the lock and unlock to the
sentry object for the stream and use that sentry object in
your insertion and extraction functions.
That doesn't solve the problem for things like:
logstream << "whatever = " << whatever << std::endl ;
Another thread can still intervene between the different <<
operators.
I use a wrapper for this, with a template operator<< which
forwards to the actual stream. The constructor of the wrapper
acquires the lock, and the destructor frees it. (Since the
wrapper will usually be used as a temporary, the lock will be
freed at the end of the full expression.)
Something like (off the top of my head):
class LockedStream
{
public:
LockedStream( ostream& dest, Mutex& mutex )
: myStream( dest )
, myMutex( mutex )
, myCounter( new int( 1 ) )
{
myMutex->lock() ;
}
LockedStream( LockedStream const& other )
: myStream( other.myStream )
, myMutex( other.myMutex )
, myCounter( other.myCounter )
{
++ (*myCounter) ;
}
~LockedStream()
{
-- (*myCounter) ;
if ( *myCounter == 0 ) {
myMutex->unlock() ;
delete myCounter ;
}
}
template< typename T >
LockedStream& operator<<( T const& obj )
{
myStream << obj ;
return *this ;
}
LockedStream& operator<<( std::ios& (*manip)( std::ios ))
{
myStream << manip ;
}
LockedStream& operator<<( std::ostream& (*manip)
( std::ostream ) )
{
myStream << manip ;
}
LockedStream& operator<<( char const* s )
{
myStream << s ;
}
private:
ostream& myStream ;
Mutex& myMutex ;
int* myCounter ;
} ;
You can then write something like:
LockedStream( myStream ) << "whatever = " << whatever <<
std::endl ;
and hold the lock for the entire duration.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34