This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: PR 5432: Thread safety patches


On Tue, Jan 22, 2002 at 06:25:32PM -0800, Benjamin Kosnik wrote:
> 
> > The sentry constructor has to block if another sentry object is bound 
> > to the same stream, and then wake up when the destructor for the other 
> > sentry object completes.   It has to be a counting mutex because some 
> > operators << and >> use others underneath.
> 
> Ok. Thanks. I guess I'm now confused by "counting mutex." Is this a 
> combination of the atomic operations for string and the gthr mutex bits 
> (ie, pthreads)? Or is this just pthreads? Sorry in advance if I'm being 
> dense.
> 
> If
> 
>   template<typename _CharT, typename _Traits>
>     basic_ostream<_CharT, _Traits>::sentry::
>     sentry(basic_ostream<_CharT,_Traits>& __os)
>     : _M_ok(__os.good()), _M_os(__os)
>     {

+       ::pthread_mutex_lock(&__os._M_mutex);
!       if (this->_M_ok && __os.tie())
> 	__os.tie()->flush();  
>     }
> 
> is the sentry ctor, what did you have in mind?

As above (given pthreads) and corresponding unlock in the destructor,
and similarly for the other istream::sentry ctor/dtor.  I don't know 
anything about other thread libraries, and precious little about 
pthreads itself, but according to various man pages...:

The basic_ios<> constructor needs to initialize a member _M_mutex
using pthread_mutexattr_init() to choose the PTHREAD_MUTEX_RECURSIVE_NP
option, which appears to be how pthreads describes a counting mutex.

Of course all this stuff should be abstracted a bit, eventually, but 
I figure that can wait until after it is ported to another thread 
library that can do the same work.

Sentry work is concerned with sharing particular iostream objects 
among threads, where Craig's work is necessary to allow sharing the 
iostream library itself among threads.  IOW, I think Craig's changes 
are completely orthogonal to this work, fix a serious bug, and ought 
to go in *immediately*.  Arguably it should go into 3.0.x too, if we've 
advertised 3.0 as thread-safe-as-long-as-you-lock-shared-objects.

Nathan Myers
ncm at cantrip dot org


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]