Search code examples
c++jsonmultithreadinggithubnlohmann-json

JSON for Modern C++ thread safe?


I am using a library named "JSON for Modern C++" (https://github.com/nlohmann/json) which is pretty slick, letting me use JSON configuration files by a C++ program that are shared with a Javascript server side application. This library essentially creates another datatype that is accessed and manipulated in way that is very close to the same as a Javascript JSON objects.

My question is, do I need to be concerned about thread safety on JSON variable accesses and manipulations or can I trust the library is thread safe. I've looked in the documentation and I don't see it say it is thread safe but I also don't see anywhere that says it isn't thread safe.

Is anyone else using this library in a multithreaded environment? Did you need to protect it yourself or did the library protect itself. Maybe I'm really lucky and the repository author nlohmann will answer directly!

Any help is greatly appreciated!


Solution

  • nlohmann library is NOT thread safe. Take a look at the header file. It's a single one. There's no mutexes, locks or atomics or anything related to threads.

    https://github.com/nlohmann/json/blob/develop/src/json.hpp

    You are responsible for protecting against concurrency of multiple threads accessing this data.