本文介绍了当在多个线程中使用read_json而不与boost.thread库链接时,是否可以始终使用BOOST_SPIRIT_THREADSAFE标志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在项目中使用了boost.我们没有链接任何boost库,但包含了boost/property_tree/ptree.hpp之类的boost头文件.

我们从多个线程(不是boost线程,而是posix线程)调用read_json,并且在read_json()函数中崩溃.现在,在包含头文件之前,我们包含了BOOST_SPIRIT_THREADSAFE,因为boost json解析器不是线程安全的,并且一切正常.但是我们的审阅者不接受此更改,他指向的是以下链接

http://www.boost. org/doc/libs/1_60_0/libs/spirit/classic/doc/grammar.html

此页面提到另一方面,如果语法打算在多线程代码中使用,然后我们应该定义包含所有Spirit头文件之前,请先输入BOOST_SPIRIT_THREADSAFE.在这个情况下,还需要链接到Boost.Threads".

但是我们真的需要链接到Boost.Threads库,因为我们没有使用Boost线程,而我的理解是Boost线程内部将在Linux平台上使用posix线程.如果我错了,可以让任何人告诉我.

解决方案

审阅者正在链接到 Classic Spirit的"1.60.0"文档.... /p>

精神经典已经过时了十多年.

此外,Boost属性树重写了它的解析器:在1.60.0中,它完全不使用Spirit .某些版本就是这种情况.

请注意,在main入口点之外使用属性树时可能会出现问题,例如:

We are using boost in our project. We are not linking any boost libraries but we are including boost header files like boost/property_tree/ptree.hpp.

We are calling read_json from multiple threads (not boost threads but posix threads) and we are getting crashes at read_json() function. Now we included BOOST_SPIRIT_THREADSAFE before including header files as boost json parser is not thread safe and every thing is working fine. But our reviewer is not accepting this change and he is pointing to the below link

http://www.boost.org/doc/libs/1_60_0/libs/spirit/classic/doc/grammar.html

As this page mentioned "On the other hand, if a grammaris intended to be used in multithreaded code, we should then defineBOOST_SPIRIT_THREADSAFE before including any spirit header files. In thiscase it will also be required to link against Boost.Threads"

But do we really need to link with the Boost.Threads library as we are not using boost threads and my understanding is boost threads internally will use posix threads on Linux platform. Can any one please let me know if I am wrong.

解决方案

The reviewer is linking to "1.60.0" documentation... of Classic Spirit.

Spirit classic has been obsolete for a decade or more.

What's more, Boost Property Tree has rewritten it's parsers: it doesn't use Spirit at all in 1.60.0. This has been the case for some versions.

Note there can be issues when using Property Tree outside the main entry-point, see e.g.:

这篇关于当在多个线程中使用read_json而不与boost.thread库链接时,是否可以始终使用BOOST_SPIRIT_THREADSAFE标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 02:09