As I understand, if I need call from C into q’s main loop, I’ll need to use sd0/sd1.
But may I understand what the parameter of type I mean? The sample code given are for Linux, where sys/eventfd.h is used. How about Win32? What kind of handle can I pass into sd0/sd1?
Hi,
The parameter of type I is just an integer. This integer is supposed to be a valid file descriptor
https://en.wikipedia.org/wiki/File_descriptor or the Windows equivalent (file handles/socket descriptors).
This abstraction can behave a bit differently on Unix and Windows systems however (Windows handles
are not usable in the all the same contexts as the unix ones).
For example on Windows, you can take a winsock socket and cast it to an int to listen for messages:
e.g
SOCKET sock = CreateSocket();
sd1((I) sock, ProcessUpdate);
and then to stop listening when you are done:
sd0((I) sock);
Note that sd0 will close the file descriptor. You may want to look at sd0x which takes a second integer
parameter that specifies whether to close the file descriptor.
Thanks
Mark Rooney
Financial Software Developer
AQUAQ Analytics
Thanks for the insight, Mark. Does this mean that I’ll need to reserve some ports (potentially a lot of them if I’m to make use of sd1 extensively) for my application? Since the I argument is to be sockets, I’ll need to basically run a lot of TCP sockets locally for this purpose?
The use case I have is that I’m having interfacing with an asychronous API where results for each function call are always returned via a callback function in some thread other than the main thread. Thus, in order to pass the result back to q, I cannot call k(0, …, (K)0) directly. I was trying to use sd0/sd1 in order to call k(0, …, (K)0) in the main thread, but I’m facing two problems:
-
Managing one socket per async function call seems to be quite a daunting task.
-
As explained in the cookbook, K objects allocated in one thread shall not be freed on another thread. Therefore, I assume that it’d be unsafe if I need to convert the result from the async API into K object before invoking the callback (since the conversion will be allocating K objects in the async thread instead of the main thread)?
How can I solve the above two problems in a manageable manner?
a wise man once pointed me to
https://github.com/ncm/selectable-socketpair
which was useful for me, but might not be exactly what you want.
Thanks for the pointer, effbiae.
It doesn’t solve my problem exactly, but it provides a great help pointing to the right direction. I have to say that sd0/sd1 is indeed quite troublesome on Win32 in this sense. But thanks to your help, I’ve managed to woked out a solution using socket pairs.