Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is boost::property_tree::ptree thread safe?

Tags:

I'm using boosts read_json in a couple of threads in a piece of code. A simplified breakdown of the call is below. I'm getting segfaults in one of the threads (and sometimes the other) and it's making me think that read_json is not thread safe (Or I'm just using it in a dumb way)

void someclass::dojson() {    using boost::property_tree::ptree;    ptree pt;    std::stringstream ss(json_data_string);     read_json(ss,pt);  } 

Now json_data_string is different between the two classes (it's just json data received over a socket).

So is read_json thread safe or do I have to mutex it (rather not) or is there a better way of calling read_json that is thread safe?

like image 281
Ross W Avatar asked Nov 16 '11 18:11

Ross W


1 Answers

Because boost json parser depends on boost::spirit, and spirit is not thread-safety default.

You can add this macro before any ptree header file to resolve it.

#define BOOST_SPIRIT_THREADSAFE #include <boost/property_tree/ptree.hpp> #include <boost/property_tree/json_parser.hpp> 
like image 166
Wei Avatar answered Sep 28 '22 22:09

Wei