cancel
Showing results for 
Search instead for 
Did you mean: 

C API, calling k within a thread

jlucid
Contributor

I am running a single threaded C++ program which publishes tables A and B.

I am using the k function below to publish the updates asynchronously.

   k(-handle, (S)".u.upd", ks((S)table_name), data, (K)0)

 

I am not explicitly calling r0(data) on the data because calling the k function decrements the reference count. 

I have confirmed this to be the case by running m4(0) before and after the call to k.

My question is, do I need to change anything in terms of memory management if I want to move the publishing of tables A and B into separate threads? Is it ok to call k from within a thread? Can both threads call k at the same time for instance? Just want to be aware of the dangers if any

 

Thanks

 

3 REPLIES 3

gyorokpeter-kx
Contributor
Contributor

How is the thread started? It is only safe to call k(...) from a thread started by q itself. So it's OK to call from C functions called by .z.pg regardless of multithreaded input mode, but it should not be called from a std::thread.

Ok thanks, so to clarify, we are not calling k from a q process, via a shared library. We are running a C++ process where different threads are responsible for publishing data to different q processes using the k function. The threads are created using std::thread. Before the threads are created, the handles to the different q processes are opened and then subsequently passed to the threads to use during the async publish.

This has been working well for us so far, in that data is being received by the q processes, and we can see that m4 (which returns the memory for the current thread) is showing no memory leaks. Are you saying there could be a danger with this setup? 

We have been looking at the API reference doc, but it doesn't mention this restriction in regards to k, see https://code.kx.com/q/interfaces/capiref/#k-evaluate. Please let us know if there is another reference we should be using. 

 

I was just looking through old discussions,  and can came across this one

where Charlie was recommending calling k() within a thread because the call blocks until the send buffer receives the message, if the buffer is full, the call will block until it is cleared. So this would imply its ok to call k within a thread, unless I am reading this wrong. What do you think?