Concurrency problems wih Boost Property tree
Asked Answered
S

1

7

I developed a simple wrapper that encapsulates a JSONObject with Boost Property trees. The problem is a segmentation fault in this code:

void JSONObject::parse(const std::string &text)
{
    std::istringstream ss(text);
    boost::property_tree::read_json(ss, *pt);
}

A bit of context, I am using JSON for message serialization. If the program uses only one thread it works without problems. But if the program uses two threads in gives a segmentation fault in the last line of the above code.

Each thread has its own JSONObject object and none of the variables are shared between the threads. My idea is that maybe the stream is not thread safe internally.

Can anyone help me?

Snowdrop answered 3/1, 2013 at 13:53 Comment(3)
are you sure pt is not shared between threads? Have You tried making minimal application calling above function and using few threads as workers? AFAIK istringstream and read_json are thread safe so it is possible that you have bug in other place of your code...Tortilla
Might I suggest posting a sscce.org ? It would make helping you easier.Henceforth
Thanks but i found the problem.Snowdrop
S
9

I found the problem. For boost property tree to be thread safe it is necessary to add this flag:

#define BOOST_SPIRIT_THREADSAFE

After adding this flag the code run without any problems.

Thanks for the help.

Snowdrop answered 9/1, 2013 at 11:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.