I wanted to get suggestions regarding the proper way of implementing callback functions in Cassandra.
I had made some APIs using Cassandra's C++ driver. Given below is how I'd used the callback function to execute my query(l_stmt is the prepared statement and rtInsertCallback is the callback function):
CassFuture * l_query_future = cass_session_execute(RtConnectionObj::ms_session, l_stmt);
CassError l_returnCode = cass_future_set_callback(l_query_future,rtInsertCallback,NULL);
if(l_returnCode != CASS_OK)
{
printf("\n [ %s::%d ] Error \n",__FILE__,__LINE__);
}
cass_future_free(l_query_future);
Assuming, the above code is executed in Thread 1, as per my current understanding, the callback function will be executed when the future is set and that too in a separate thread(Thread 2). The callback function is like this:
void rtInsertCallback(CassFuture* l_csp_future, void *data)
{
CassError l_returnCode = cass_future_error_code(l_csp_future);
if (l_returnCode != CASS_OK)
{
printf("\n[%s::%d] %s ",__FILE__,__LINE__,cass_error_desc(l_returnCode));
}
else
{
printf("\n [%s::%d] Data Inserted successfully ...",__FILE__,__LINE__);
}
}
I want to know, is it possible that, before the future is set or before the CassError l_returnCode = cass_future_error_code(l_csp_future); statement gets executed in Thread 2, the future gets freed by Thread 1 due to which the above statement in thread 2 will be operating on a freed future? If yes, then what should be the proper way of handling such scenario? If this question does not make any sense(due to my misunderstanding of any concept), please explain. Thanks!