public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Paul Dubuc <pdubuc@cas.org>
To: libstdc++@gcc.gnu.org, gcc-bugs@gcc.gnu.org, gcc-help@gcc.gnu.org
Subject: Subtle MT problem with __gnu_cxx::hash_map
Date: Fri, 19 Nov 2004 20:21:00 -0000	[thread overview]
Message-ID: <419E55D6.6030803@cas.org> (raw)

There's a subtle thread safety problem with hash_map that was found in our 
testing recently.  It's understood that operator[] is a non-const method since 
it can insert an element into a container if one is not found with the given index.

In our case we were using operator[] to access a hash_map that had been fully 
populated.  Each index we were using did have an entry in the hash_map.  So we 
were accessing elements in the map with operator[] using multiple threads 
thinking that this would be a thread safe, const operation.  This is implied by 
the statement "simultaneous read accesses to to shared containers are safe" in 
the SGI STL user's guide (http://www.sgi.com/tech/stl/thread_safety.html).

This worked for a long time until we tried it on a hash_map with 389 elements. 
The program crashed.  A search through the source found that 389 is one of the 
size values as which a hash_map will expand:

In ext/stl_hashtable.h (for GCC 3.3.4, ext/hashtable.h for GCC 3.4.3):

static const unsigned long __stl_prime_list[__stl_num_primes] =
{
   53ul,         97ul,         193ul,       389ul,       769ul,
   1543ul,       3079ul,       6151ul,      12289ul,     24593ul,
   49157ul,      98317ul,      196613ul,    393241ul,    786433ul,
   1572869ul,    3145739ul,    6291469ul,   12582917ul,  25165843ul,
   50331653ul,   100663319ul,  201326611ul, 402653189ul, 805306457ul,
   1610612741ul, 3221225473ul, 4294967291ul
};

Sure enough the problem happens with hash_tables of other sizes in this table. 
The find_or_insert() method in stl_hashtable.h seems to call resize() before it 
really needs to.  The problem could be fixed by moving the call to resize to be 
after the first return statement:

template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
typename hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>::reference
hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>::find_or_insert(const value_type& __obj)
{
   resize(_M_num_elements + 1);	// <- FROM HERE

   size_type __n = _M_bkt_num(__obj);
   _Node* __first = _M_buckets[__n];

   for (_Node* __cur = __first; __cur; __cur = __cur->_M_next)
     if (_M_equals(_M_get_key(__cur->_M_val), _M_get_key(__obj)))
       return __cur->_M_val;
				// <-- TO HERE
   _Node* __tmp = _M_new_node(__obj);
   __tmp->_M_next = __first;
   _M_buckets[__n] = __tmp;
   ++_M_num_elements;
   return __tmp->_M_val;
}


One could argue that find() should always used to access the hash_map, but using 
operator[] is often more succinct and readable in complex code.  The problem is 
easy to fall into, difficult to diagnose, hard to reproduce (unless you know the 
magic prime numbers) and apparently easy to fix (unless I'm missing something). 
  Could this be changed in the next release of the library?  It might fix or 
prevent some time bombs out in the field.

Thanks,

Paul Dubuc

             reply	other threads:[~2004-11-19 20:21 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-11-19 20:21 Paul Dubuc [this message]
2004-11-19 20:31 ` Matt Austern
2004-11-19 20:45   ` Paul Dubuc

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=419E55D6.6030803@cas.org \
    --to=pdubuc@cas.org \
    --cc=gcc-bugs@gcc.gnu.org \
    --cc=gcc-help@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).