"Keith H Duggar" <duggar@alum.mit.edu> wrote in message
news:4be295e6-b63f-4876-a796-f824d3495275@22g2000vbg.googlegroups.com...
However it also makes thread-safety more difficult for
clients of getOrZero and therefore IMO is not appropriate
for such a library function. Instead I use an overload of
getOrZero which follows the Tao of POSIX reentrant support:
template < class K, class V, class C, class A >
V const &
getOrZero (
std::map<K,V,C,A> const & m
, K const & k
, V const & zero
) {
typename std::map<K,V,C,A>::const_iterator i = m.find(k) ;
return i != m.end() ? i->second : zero ;
}
Unless I am missing something obvious I am fairly sure that most
implementations of map are not thread-safe so calling find() in your
version above is also not thread-safe (one thread could modify the map
whilst the other thread is searching it) so a lock must still be acquired
making adding a zero parameter instead of using a shared static seem
pointless (assuming I am not missing something obvious).
different threads.