Hi Eric,
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