kazuho/picojson

Consider removing static objects

Opened this issue · 4 comments

Static objects (for example string returned by picojson::get_last_error) disqualify the library from multithreaded applications.

Unless you do not use operator>> then there would not be any issues regarding multi-threading. The static null objects are never modified / never should be modified.

If you are interested in fixing the former, please open a PR.

I would like to fix this issue with static object. I think there are following possible ways for the fix:

  • operator>> will throw exception instead of setting of global object. This is probably most C++-way, but it introduces change in the library interface.
  • thread_local from C++11. But compatibility with older versions of C++ is probably needed.
  • Some thread local storage, eg. from posix threads.

Does some of the solutions work for you?

@hagrid-the-developer IMO we should maintain the compatibility of the interface and the portability of the code.

That said, may I ask why you want to fix the issue?

If you want MT-safe way to deserialize the values, you can use the picojson::parse function, as desribed in README:

std::istream_iterator input(std::cin);
picojson::value v;
std::string err;
input = picojson::parse(v, input, std::istream_iterator(), &err);
if (! err.empty()) {
  std::cerr << err << std::endl;
}
bver commented

It would be nice to deprecate the get_last_error() call in favor of the solution # 1 suggested by @hagrid-the-developer and introduce the interface change later in the 2.x.x version of picojson. But we have been sufficiently warned to avoid non-MT stuff in MT code so it is OK as it is.
Anyway, thanks for the great library.