g++ stringstream 构造函数是否有临界区?
Does the g++ stringstream constructor have a critical section?
我正在查看一些使用 openmp 并行化循环的源代码。在循环内部构造了一个字符串流,并向其中写入了一些字符,最后使用 stringstream::str() 检索字符。循环的并行化似乎对性能没有太大影响,直到将 stringstream 构造函数移出循环并替换为对 stringstream::clear().
的调用
stringstream 构造函数中是否存在临界区或其他阻塞机制?如果是这样,那是否记录在某处?代码是用 g++ 4.9.2 编译的。
销毁 stringstream
对象会销毁其内存缓冲区。您正在看到循环内堆分配的影响。
调用clear
不会释放任何内存。它允许循环重用缓冲区。
std::stringstream
构造函数调用std::locale
.
的默认构造函数
这是因为它继承自 std::ios_base
并且此类类型必须调用 std::basic_ios::init()
(如此处所述:https://en.cppreference.com/w/cpp/io/ios_base/ios_base). This call honors the post-condition that getloc()
will then return a copy of a default constructed std::local
object (as described here https://en.cppreference.com/w/cpp/io/basic_ios/init)- 因此 std::locale()
必须在 [= 内调用10=]的构造函数。
std::local
构造函数将导致与当前全局语言环境相关的方面的引用计数增加。 (参见 https://en.cppreference.com/w/cpp/locale/locale)。
both the locale reference count and each facet reference count are updated in thread-safe manner, similar to std::shared_ptr
增量必须是原子的,因为它正在修改共享状态。
根据您使用的标准库实现,这可以解释为什么您没有看到预期的性能提升。
我正在查看一些使用 openmp 并行化循环的源代码。在循环内部构造了一个字符串流,并向其中写入了一些字符,最后使用 stringstream::str() 检索字符。循环的并行化似乎对性能没有太大影响,直到将 stringstream 构造函数移出循环并替换为对 stringstream::clear().
的调用stringstream 构造函数中是否存在临界区或其他阻塞机制?如果是这样,那是否记录在某处?代码是用 g++ 4.9.2 编译的。
销毁 stringstream
对象会销毁其内存缓冲区。您正在看到循环内堆分配的影响。
调用clear
不会释放任何内存。它允许循环重用缓冲区。
std::stringstream
构造函数调用std::locale
.
这是因为它继承自 std::ios_base
并且此类类型必须调用 std::basic_ios::init()
(如此处所述:https://en.cppreference.com/w/cpp/io/ios_base/ios_base). This call honors the post-condition that getloc()
will then return a copy of a default constructed std::local
object (as described here https://en.cppreference.com/w/cpp/io/basic_ios/init)- 因此 std::locale()
必须在 [= 内调用10=]的构造函数。
std::local
构造函数将导致与当前全局语言环境相关的方面的引用计数增加。 (参见 https://en.cppreference.com/w/cpp/locale/locale)。
both the locale reference count and each facet reference count are updated in thread-safe manner, similar to std::shared_ptr
增量必须是原子的,因为它正在修改共享状态。
根据您使用的标准库实现,这可以解释为什么您没有看到预期的性能提升。