# sd0/sd1 in Win32?

**URL:** <https://forum.kx.com/t/sd0-sd1-in-win32/9566>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [March 3, 2015, 10:52am UTC](https://forum.kx.com/t/sd0-sd1-in-win32/9566 "2015-03-03T10:52:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Flying1](https://avatars.discourse-cdn.com/v4/letter/f/f4b2a3/32.png) [@Flying1](https://forum.kx.com/u/Flying1)\
**Post date:** [March 3, 2015, 10:52am UTC](https://forum.kx.com/t/sd0-sd1-in-win32/9566/1 "2015-03-03T10:52:00Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![mark\_rooney](https://avatars.discourse-cdn.com/v4/letter/m/278dde/32.png) [@mark\_rooney](https://forum.kx.com/u/mark_rooney)\
**Post date:** [March 3, 2015, 11:43am UTC](https://forum.kx.com/t/sd0-sd1-in-win32/9566/2 "2015-03-03T11:43:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Flying1](https://avatars.discourse-cdn.com/v4/letter/f/f4b2a3/32.png) [@Flying1](https://forum.kx.com/u/Flying1)\
**Post date:** [March 4, 2015, 5:46am UTC](https://forum.kx.com/t/sd0-sd1-in-win32/9566/3 "2015-03-04T05:46:00Z")

</div>

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:

1. Managing one socket per async function call seems to be quite a daunting task.

2. 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?

---

<div class="post-metadata">

**Author:** ![effbiae](https://avatars.discourse-cdn.com/v4/letter/e/ecae2f/32.png) [@effbiae](https://forum.kx.com/u/effbiae)\
**Post date:** [March 4, 2015, 9:58am UTC](https://forum.kx.com/t/sd0-sd1-in-win32/9566/4 "2015-03-04T09:58:00Z")

</div>

a wise man once pointed me to  
&nbsp;&nbsp;https://github.com/ncm/selectable-socketpair

which was useful for me, but might not be exactly what you want.

---

<div class="post-metadata">

**Author:** ![Flying1](https://avatars.discourse-cdn.com/v4/letter/f/f4b2a3/32.png) [@Flying1](https://forum.kx.com/u/Flying1)\
**Post date:** [March 6, 2015, 12:41am UTC](https://forum.kx.com/t/sd0-sd1-in-win32/9566/5 "2015-03-06T00:41:00Z")

</div>

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.
