0

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!

1 Answer 1

4

You are free to release the future in "Thread 1" after the callback statement. Callback will have its own copy of the future to work upon.

The Cassandra callback function will also take care of resource cleanup after it gets processed. So we don't have to explicitly free the future.

Code Sample can be found here

https://github.com/datastax/cpp-driver/blob/master/examples/callbacks/callbacks.c

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.