stringstream segmentation violation in a multithreaded program
911936Jan 18 2012 — edited Jan 19 2012Hello,
I've developed a small program to illustrate the problem we are facing with stringstream-s in SunStudio 12, more exactly using stringstream in a multithreaded program leads to crashes even if the stringstream is a local object not shared between threads:
-------------
// stringstream_test.C
#include <pthread.h>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
void ThreadFunc(void ID)
{
const char* testData = "Data data data\ndata1 data1 data1\ndata2 data2 data2";
printf("Running test thread ... \n");
while(true)
{
stringstream sStream(testData);
string tmp;
sStream >> tmp;
sStream >> ws;
sStream >> tmp;
sStream >> ws;
string t2;
sStream >> t2;
string t3;
getline(sStream, t3);
string t5;
for(int i = 0; i < 10; ++i)
{
string t4;
sStream >> t4;
getline(sStream, t5);
}
}
return 0;
}
int main(int argc, char** argv)
{
int threads = 10;
if(argc > 1)
threads = atoi(argv[1]);
printf("Starting %d threads \n", threads);
for(int i = 0; i < threads; ++i)
{
pthread_t thr;
pthread_create(&thr, 0, &ThreadFunc, (void*) i);
}
printf("\nPress Ctrl-C to exit:");
getchar();
printf("Exiting ... \n");
return 0;
}
-----------------
I'm compiling it with:
CC -mt -o strstream_test -lpthread strstream_test.C
Here is some information about the environment I use:
CC -V
CC: Sun C++ 5.9 SunOS_sparc Patch 124863-03 2008/03/12
uname -a
SunOS sun11bld 5.10 Generic_141444-09 sun4u sparc SUNW,Sun-Fire-V490
showrev -p | grep SUNWlibC
Patch: 119963-16 Obsoletes: Requires: Incompatibles: Packages: SUNWlibC
When running the above program I'm getting a core dump with the following stack:
------------------------------
pflags core
core 'core' of 22087: ./strstream_test
data model = _ILP32 flags = MSACCT|MSFORK
/8: flags = 0
sigmask = 0xffffbefc,0x0000ffff cursig = SIGSEGV
core 'core' of 22087: ./strstream_test
----------------- lwp# 8 / thread# 8 --------------------
ff27bddc void std::ios::init(std::streambuf *) (fe87bf38, 4d58, 4d59, 3, ff37ddc4, 23580) + a4
ff27bd00 std::ios::basic_ios #Nvariant 1() (fe87bf38, ff380510, 210, ff37ddc4, 102104, 0) + 44
ff2d3c60 std::stringstream::basic_stringstream(const std::string &,int) (fe87bec8, fe87bec4, c, ff382030, aa1a8, ff37ddc4) + 48
00011b88 void*ThreadFunc(void*) (6, a, fe87bec8, a, fe87bec8, fe87bec8) + 40
ff048a20 lwpstart (0, 0, 0, 0, 0, 0)
-------
In our software using stringstream-s we get similar crashes related either to the construction or destruction of the stringstream class or the locale object (std::locale::init).
Is this something known? Do I miss any build options?
I've tried to look at the patches available for SunStudio, but didn't find anything which could be related to this.
Thanks,
Octav