json-c-0.11 Thread safe?

77 views
Skip to first unread message

Mr.K

unread,
Jul 22, 2021, 2:23:08 PM7/22/21
to json-c
Hi Team,

Need a clarification.
Currently I am working with json-c-0.11
I am planning to use it in multithreaded program. Is it safe to use it in multi threaded programs?

Thanks
Kalai

Eric Hawicz

unread,
Jul 22, 2021, 2:38:00 PM7/22/21
to jso...@googlegroups.com
Do not use 0.11, it's ancient (8+ years old now) and there have been many bug fixes since then.
It might work, with the same caveats as https://groups.google.com/g/json-c/c/nfzf3XQWhng, but we're certainly not going to fix anything with that version if it doesn't work.

Eric

Mr.K

unread,
Jul 23, 2021, 3:30:29 PM7/23/21
to json-c
Hi Eric,

Thanks for your reply. I have downloaded the json-c-0.15 from https://github.com/json-c/json-c/tree/json-c-0.15 and have started building it.
I have observed few issues and I will send out a separate mail for clarification.

With respect to multi threading, my threads will have their own objects to work on and will not interfere with others.
Basically I am just converting the binary data to JSON. The below is a sample program which mimics our implementation.
Hopefully there is no issue with multithreading in below program even with json-c-0.11.
Also do we need to compile our code with ENABLE_THREADING option to be thread safe?

#include <json/json.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>

#define MAX_THREADS 10
#define MAX_MEMBERS 4
#define MAX_HOUSE 10
void create_json(json_object **streetarray)
{
        int i,j;
        for(i=0; i<MAX_HOUSE; i++)
        {
                json_object *houseobj = json_object_new_object();
                json_object_object_add(houseobj,"HouseName", json_object_new_string("Rose cottage"));
                json_object_object_add(houseobj,"NoOfMembers", json_object_new_int(MAX_MEMBERS));
                json_object_object_add(houseobj,"DoorNo", json_object_new_int(MAX_MEMBERS));

                json_object *membersarray= json_object_new_array();
                for(j=0; j<MAX_MEMBERS; j++)
                {
                        json_object *jobj = json_object_new_object();
                        json_object_object_add(jobj,"FirstName", json_object_new_string("Pat"));
                        json_object_object_add(jobj,"LastName", json_object_new_string("cummins"));
                        json_object_object_add(jobj,"Age", json_object_new_int(j+30));
                        json_object_array_add(membersarray, jobj);
                }
                json_object_object_add(houseobj, "members",membersarray);
                json_object *parent_houseobj = json_object_new_object();
                json_object_object_add(parent_houseobj, "house", houseobj);
                json_object_array_add(*streetarray, parent_houseobj);
        }
}
void* thread_main(void* threadid)
{
        json_object *strtobj   = json_object_new_object();
        json_object *strtarray = json_object_new_array();
        create_json(&strtarray);
        json_object_object_add(strtobj,"street", strtarray);
        printf("Thread %d prints\n %s\n", (int)threadid, json_object_to_json_string(strtobj));
        json_object_put(strtobj);
        return NULL;
}
int main()
{
        int i=0;
        pthread_t tid[MAX_THREADS];
        memset(&tid, sizeof(tid), 0);
        for(i=0; i<MAX_THREADS; i++)
        {
                int ret = pthread_create(&tid[i], NULL, thread_main, (void *)i);
                if(ret == 0)
                {
                        printf("Thread %d created\n",i);
                } else {
                        printf("failed to create thread %d\n",i);
                }
        }
        for(i=0; i<MAX_THREADS; i++)
        {
                pthread_join(tid[i],NULL);
        }
}

Thanks

Eric Hawicz

unread,
Jul 24, 2021, 12:03:02 AM7/24/21
to jso...@googlegroups.com
On Fri, Jul 23, 2021, 3:30 PM Mr.K <kalaim...@gmail.com> wrote:
Hi Eric,
...
Hopefully there is no issue with multithreading in below program even with json-c-0.11.
Also do we need to compile our code with ENABLE_THREADING option to be thread safe?
Since each thread is working with an entirely different set of json objects, it should be fine.  There is no need to set ENABLE_THREADING.

Eric

Mr.K

unread,
Jul 24, 2021, 3:41:05 AM7/24/21
to json-c
Thanks Eric. 

Appreciate your effort and contribution. You are amazing.

Thanks again.

Reply all
Reply to author
Forward
0 new messages