今天一个service即将发布之前,用压力测试试了一下,很快就segmentation fault,吓了一身冷汗。
三小时候确定是因为最近几天引入boost::ptree来将解析json的时候出问题。重新自己实现解析代码后,问题解决。
单独开一个工程,在多线程的情况下调用,错误重现。下面的代码时不时就会crash。
class testBind {
public:
void testFunc() {
cout<<"ok"<<endl;
string str = "{\"51\":1,\"50\":1}";
stringstream stream;
stream<<str;
ptree tree;
read_json(stream, tree);
}
};
/*
*
*/
int main(int argc, char** argv) {
testBind tb;
boost::thread_group tg;
for (int i = 0; i < 20; ++i) {
tg.add_thread(new boost::thread(boost::bind(&testBind::testFunc, &tb)));
}
int x;
cin >> x;
tg.join_all();
return 0;
}