The use of uv_udp_send() in multi-threaded environment

686 views
Skip to first unread message

David Pajak

unread,
Oct 26, 2013, 3:40:05 PM10/26/13
to li...@googlegroups.com
Hi Guys,

I'm writing an app that will communicate with clients over the same UDP socket. Right now, I get all the packets via event loop (all the libuv calls happen from the "main" event loop thread). I would like to send back some data via uv_udp_send() from another thread. Is this operation thread-safe? Both receive and send thread would use the same uv_udp_t structure, hence my concern. If it's not possible to call uv_udp_send() directly, what is the best (as in, the highest perf) indirect way? Add custom send action via uv_async_t and send the data when the event loop calls the action handler?

Thanks,

- Dawid

Ben Noordhuis

unread,
Oct 26, 2013, 6:47:16 PM10/26/13
to li...@googlegroups.com
Using the same event loop from different threads is not safe. Very
few functions in libuv are thread-safe; if the documentation for a
function does not explicitly mention that it's thread-safe, then you
can safely assume it's not.

Using an uv_async_t handle should work with two caveats:

a) libuv can (and will) coalesce multiple calls to uv_async_send()
into a single call to the async callback, and

b) you're adding a fair amount of overhead per datagram that way so
you may not achieve very high throughput

An alternative approach would be to create two event loops, one per
thread and each with its own UDP socket. Receive datagrams in one
thread and send datagrams from the other thread, done. (Only call
uv_udp_recv_start() in one thread, of course.)

It's possible to bind both sockets to the same port by creating an
IPC-capable pipe server and using that to send over the socket to the
other thread. See test/benchmark-multi-accept.c for an example.

David Pajak

unread,
Oct 28, 2013, 4:24:37 PM10/28/13
to li...@googlegroups.com
Great answer. Thanks Ben!
Reply all
Reply to author
Forward
0 new messages