Am using the C JSON library under Ubuntu (json-c/json.h). I need to parse JSON strings on multiple POSIX threads. Am currently using the json_tokener_parse() method - is this multi-thread safe or do I need to use something else?
thnx
Am using the C JSON library under Ubuntu (json-c/json.h). I need to parse JSON strings on multiple POSIX threads. Am currently using the json_tokener_parse() method - is this multi-thread safe or do I need to use something else?
thnx
I looked through the code: https://github.com/json-c/json-c/blob/master/json_tokener.c
It appears to be thread-safe with one exception:
#ifdef HAVE_SETLOCALE
char *oldlocale=NULL, *tmplocale;
tmplocale = setlocale(LC_NUMERIC, NULL);
if (tmplocale) oldlocale = strdup(tmplocale);
setlocale(LC_NUMERIC, "C");
#endif
So if HAVE_SETLOCALE
is defined (and it probably will be), setlocale()
will be called and it will set the process-wide LC_NUMERIC
to "C"
. And of course it undoes this at the end. This will cause problems if your LC_NUMERIC
is not "C"
or a compatible locale at the beginning, because one thread will "restore" your locale while another one may still be parsing and expecting the "C"
locale to be in effect.
Fortunately it is guaranteed that the locale will be "C"
on program start, so you just need to make sure that neither you nor any other library you're using sets LC_NUMERIC
(or LC_ALL
of course) to a locale incompatible with "C"
. You could then rebuild the library with HAVE_SETLOCALE
undefined if you want, but this probably doesn't matter, as its calls to setlocale()
will have no real effect.