【发布时间】:2019-01-05 01:59:43
【问题描述】:
我遇到了在多个线程中使用 boost::format() 的问题。 boost 格式库使用 boost 解析库,它使用 /usr/include/c++/4.8/bits/locale_facets.h 中定义的函数 std::ctype::narrow() (我使用的是 G++ 版本 4.8)。
narrow() 函数不是很无害。实例变量 _M_narrow 是一个缓存。我发现跨线程同时写入和读取此缓存。必须锁定线程才能使用 boost::format 似乎很愚蠢,以至于避免使用 boost::format 这让我觉得我一定错过了一些东西。有没有人对这个问题有更深入的了解?
/**
* @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;
}
【问题讨论】:
标签: c++ multithreading boost