is boost::property_tree::ptree thread safe?(boost::property_tree::ptree 线程安全吗?)
问题描述
我在一段代码的几个线程中使用 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 线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:boost::property_tree::ptree 线程安全吗?
基础教程推荐
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- CString 到 char* 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
