stdcxx-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Martin Sebor <>
Subject thoughts on STDCXX-452 -- std::numpunct not thread safe
Date Wed, 26 Sep 2007 15:48:59 GMT
 From talking with Travis, the problem with the thread safety
of the numpunct facet is accessing (reading and writing) the
data members in the public member functions like the one below.
The code is also unsafe in the sense that the do_decimal_point()
function isn't guaranteed to be called at most once when multiple
threads call decimal_point() at the same time but that in itself
shouldn't cause any real problems since the virtual function is
(or should be) thread safe. The real problem, I think, is in the
unsafe assignment to _C_decimal_point. We need to find a binary
compatible way to assign in a thread safe way the result of the
do_xxx() functions to the member variables. We also need to OR
the flags member in a thread safe way so as to avoid clearing
(or setting) some of the other bits.

The first could be done by _RWSTD_ATOMIC_EXCHANGE():

   const string_type __dp = do_decimal_point ();

   _RWSTD_ATOMIC_EXCHANGE (__self->_C_decimal_point,
                           __dp, __self->_C_mutex);

For the second we might need to add _RWSTD_ATOMIC_OR().



template <class _CharT>
inline _TYPENAME numpunct<_CharT>::char_type
numpunct<_CharT>::decimal_point () const
     if (!(_C_flags & _RW::__rw_dp)) {

         numpunct* const __self = _RWSTD_CONST_CAST (numpunct*, this);

         // [try to] get the decimal point first (may throw)
         // then set a flag to avoid future initializations
         __self->_C_decimal_point  = do_decimal_point ();
         __self->_C_flags         |= _RW::__rw_dp;

     return _C_decimal_point;

View raw message