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

thread safety of cJSON #221

Closed
patrickfrey opened this issue Nov 28, 2017 · 3 comments
Closed

thread safety of cJSON #221

patrickfrey opened this issue Nov 28, 2017 · 3 comments

Comments

@patrickfrey
Copy link

It would be relatively easy to make cJSON thread safe. Just get rid of the global error variable passing a pointer to a context structure to the parser for the error info. I submitted a request that was ignored some years ago (around 2011). I therefore assume that it is not seen as an important issue. I patched my fork of cJSON to archieve threadsafety. But seeing, that it is so easy to add it, I would like to suggest it again.

@FSMaxB
Copy link
Collaborator

FSMaxB commented Nov 28, 2017

Thread safety is on my list for version 2. And although cJSON is not 100% thread safe at the moment, there is a section in the readme about how it can be used across threads safely.

https://github.com/DaveGamble/cJSON#thread-safety

If you use cJSON_ParseWithOpts and use return_parse_end, the global error variable is not used, so there is no issue there.

The other problems with thread safety is cJSON_InitHooks and setlocale. If you avoid these, cJSON is already thread safe.

@FSMaxB FSMaxB closed this as completed Nov 28, 2017
@FSMaxB
Copy link
Collaborator

FSMaxB commented Nov 28, 2017

It might even be possible to fix the thread safety problem with the allocators with cJSON_Configuration outlined in #177 by passing allocators to the function that creates the configuration object.

And there is a reason why parse_buffer contains internal_hooks, because the internal functions don't access global variables. This only happens in the functions that are called by the user of the library directly, the public API. They act like an adapter so that the internal functions can be rewritten in a thread safe way, so in the future I just have to change the "adapter" functions, thereby changing the public API and making it thread safe.

@FSMaxB
Copy link
Collaborator

FSMaxB commented Nov 28, 2017

That doesn't fix the problems with setlocale though, but I think this is a non issue, because most people don't change that more than once at the beginning.

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

No branches or pull requests

2 participants