Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: thread safety of read only accesses #651

Closed
kimwalisch opened this issue Jul 10, 2017 · 4 comments
Closed

Question: thread safety of read only accesses #651

kimwalisch opened this issue Jul 10, 2017 · 4 comments

Comments

@kimwalisch
Copy link

kimwalisch commented Jul 10, 2017

Hi,

Are nlohmann::json objects thread safe for read only operations? E.g. assuming that the json object is not modified anymore after load_json(), can I safely query the json object from multiple threads as in the example below?

// single threaded
auto json = load_json();

#pragma omp parallel for
for (int i = 0; i < threads; i++)
{
    // multi-threaded read only accesses
    double seconds = json["S2_easy"]["seconds"];
    int count = json["S2_easy"].count("s2_easy");
}

Thanks!

@nlohmann
Copy link
Owner

The library uses STL containers internally; that is, std::vector for JSON arrays and std::map for JSON objects. Very brief googling gives me the feeling that concurrent reads should be fine.

@kimwalisch
Copy link
Author

Thanks!

@nlohmann
Copy link
Owner

If you experience any issues, please let me know! I had no reports so far about multithreading applications.

@kimwalisch
Copy link
Author

If you experience any issues, please let me know! I had no reports so far about multi-threading applications.

My experiences:

I am using nlohmann::json in my math library to backup intermediate results of long running computations. In the resume step (used e.g. after a crash) multiple threads read values from a single nlohmann::json object (none of the threads modifies the object).

Everything works as expected, there are no issues. I don't use any mutexes or locks during the resume step. I have tested the code using valgrindand no issues have been detected.

Greetings,
Kim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants