问题描述
我在一段代码中的几个线程中使用了boosts read_json.通话的简化分类如下.我在其中一个线程中(有时在另一个线程中)出现段错误,这让我认为read_json不是线程安全的(或者我只是以一种愚蠢的方式使用它)
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);
}
现在,两个类之间的json_data_string是不同的(只是通过套接字接收的json数据).
Now json_data_string is different between the two classes (it's just json data received over a socket).
那么read_json线程是安全的还是我必须对其进行互斥(而不是互斥),或者是否有更好的方法来调用线程安全的read_json?
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?
推荐答案
因为boost json解析器依赖于boost :: spirit,而spirit不是默认的线程安全性.
Because boost json parser depends on boost::spirit, and spirit is not thread-safety default.
您可以在任何ptree头文件之前添加此宏来解决它.
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>
这篇关于boost :: property_tree :: ptree线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!