I was looking over some source code that used openmp to parallelize a loop. Inside the loop a stringstream was constructed and a few characters were written to it, and at the end the characters were retrieved using stringstream::str(). The parallelization of the loop didn't seem to make much difference to performance, until the stringstream constructor was moved out of the loop and replaced with calls to stringstream::clear().
Is there a critical section or some other blocking mechanism inside the stringstream constructor? If so, is that documented somewhere? The code was compiled with g++ 4.9.2.
Destroying a stringstream
object destroys its memory buffer. You are seeing the effects of heap allocation inside the loop.
Calling clear
does not free any memory. It allows the loop to reuse the buffer.