Hi All,
I have scenario where my callback function data_update() can be called anytime. I have written the function data_update() such that it will create detached thread for processing the data sent to this function.
data_update()
{
pthread_attr_t attr_thread;
pthread_attr_init(&attr_thread);
if ( pthread_attr_setdetachstate(&attr_thread, PTHREAD_CREATE_DETACHED) != 0 )
{
perror("pthread_attr_setdetachstate: ");
return;
}
thrdArgObj = (threadArg *)malloc(sizeof(thrdArg));
/* create argument for thread */
threadArgObj->ClientLst = clntlst;
if ( pthread_create(&threadId, &attr_thread, (void *)handleAsynchData, (void *)thrdArgObj) != 0 )
{
perror("pthread_create: ");
return;
}
(void) pthread_attr_destroy(&attr_thread);
}
void *
handleAsynchData(void *ptr)
{
thrdArgObj *thrdArg = (thrdArgObj *)ptr;
(void)process_data(thrdArg->ClientLst);
pthread_exit(NULL);
}
In the function handleAsynchData() I am calling another function process_data() to which the member of the structure threadArgObj is passed. Based on the client list, the function process_data() spends time in execution. It can be very short time for some list and a long time for some list. This function in turn, process the data and returns back. Here, since the function the function data_update() is a call back function it can be called at any time. This is the reason I have created detached thread. Also, I am making use og mutec for synchronization in the function process_data().
But, when I run my program. It runs fine for some time and then the program crashes. This I see consistently. Am I doing something wrong in the way I am creating detached thread or free attributes.
Please provide inputs