I came across what appears to be a problem with using boost::format() in multiple threads. The boost format library uses the boost parse library which uses the function std::ctype::narrow() defined in /usr/include/c++/4.8/bits/locale_facets.h (I am using G++ version 4.8).
The narrow() function is not very innocuous. The instance variable _M_narrow is a cache. I found that across threads this cache was being written to and read from at the same time. Having to lock threads to use boost::format seems silly enough to avoid using boost::format which makes me think I must be missing something. Does anyone have any more insight into this problem?
/**
* @brief Narrow char
*
* This function converts the char to char using the simplest
* reasonable transformation. If the conversion fails, dfault is
* returned instead. For an underived ctype<char> facet, @a c
* will be returned unchanged.
*
* This function works as if it returns ctype<char>::do_narrow(c).
* do_narrow() must always return the same result for the same input.
*
* Note: this is not what you want for codepage conversions. See
* codecvt for that.
*
* @param __c The char to convert.
* @param __dfault Char to return if conversion fails.
* @return The converted character.
*/
char
narrow(char_type __c, char __dfault) const
{
if (_M_narrow[static_cast<unsigned char>(__c)])
return _M_narrow[static_cast<unsigned char>(__c)];
const char __t = do_narrow(__c, __dfault);
if (__t != __dfault)
_M_narrow[static_cast<unsigned char>(__c)] = __t;
return __t;
}