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: Patch ping


Or another option would be to eliminate the possible memory leak in
some other way. The possibility of the memory leak arises because
two threads may call operator() on a __use_cache simultaneously. When this happens, it is possible for both threads to test that
__caches[__i] is not set, and for both to set it to a new cache
object. In a preemptive threading model, the threads could switch
right at the assignment in _M_install_cache, so it seems
theoretically possible for the value stored in _M_caches[__index] to
be confused by a simultaneous assignment from two threads. I don't
see this can be safely avoided without using a mutex.

Can't this be solved using atomic_compare_exchange (I'm not sure of its exact spelling, or whether it's currently part of libstdc++)?


for(;;)
{

if( atomic_load_acquire( __caches[i] ) == 0 )
{
   p = new cache;

   if( !atomic_compare_exchange_release( &__caches[i], 0, p ) )
   {
       // another thread has written to __caches[i] in the meantime
       delete p;
   }
   else
   {
       break;
   }
}

}

or something along those lines.


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