Hi all!
I have a question about thread synchronization and c++ streams
(iostreams, stringstreams, etc). When optimizing a program for a
multicore processor I found that stringstream was causing a LOT of
synchronization overhead. After a bit of digging I concluded that this
synchronization has to do with the access to a global locale inside
the stream.
The problem can be seen by running the small distilled benchmark code
that compares the threaded performance of stringsream with sprintf:
http://www.optonova.se/asu/main.cpp
From the screen shots of a run with the Intel Thread Profiler one can
clearly see the destructive synchronization behavior (yellow lines) :
http://www.optonova.se/asu/overview.png
http://www.optonova.se/asu/stringstream_zoom.png
http://www.optonova.se/asu/sprintf_zoom.png
In this simple example, stringstream is approximately 2000 times
slower than sprintf!
This was all very interesting, but what I really would like to know is
how to get around the synchronization (I would really like to use
stringstream)?
I'm using the MS Visual Studio 2005 compiler and running the app. on
Windows XP boxes.
Any help will be much appreciated.
Best Regards,
Anders Sundman