Transfer a tcp handle between main thread and worker thread

Skip to first unread message

Ariel Machado

Jul 6, 2020, 2:11:44 PM7/6/20
to libuv
Hi folks!

I would like to know the correct way to pass the tcp handle of a connection to another thread that will read / write this handle.

The idea is, on the main thread, listen and accept new connections. These connections are validated using a handshake protocol on this same thread and if everything is ok, that connection is added to a queue that will be processed by another thread.

Currently in raw sockets I can simply pass the socket file descriptor between threads, but in libuv I've been seeing examples where it uses IPC pipes and uv_write2.
But in my case what I need to do is a inter-thread handle communication but not an inter-process communication.
I think that the boilerplate and overhead of ipc pipes, (listen, write, read ipc's) just to pass a handle within the same process should not be the correct approach ... or should it?
I would like  to get your feedback.

Thank you!

Tom Zhou

Jul 6, 2020, 3:22:55 PM7/6/20
AFAIK, libuv is using non-blocking and single thread IO model. No need handle tcp connection in another thread. 
if you want to utilize multiple-core CPU, you can try spawn multiple libuv process instead of multiple thread to handle tcp io.

Best regards
  tom zhou

You received this message because you are subscribed to the Google Groups "libuv" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit

Ariel Machado

Jul 7, 2020, 5:12:19 AM7/7/20
to libuv
Hi Tom,
In this specific case, it is not about using multi-core CPUs, but about not blocking the main thread with heavy processing tasks, nor passing request / response data for read / write operations between threads.
In addition, spawn multiple process is not always applicable when what you want it to process a FIFO queue of connections sequentially, deals with a single resource like a DB connection, etc.
What I need is a lightweight way to pass an handle of an accepted tcp connection between threads, unregistering it from the main loop an registering it in the worker thread loop.

tom zhou

Jul 9, 2020, 4:56:35 AM7/9/20
to libuv
Hi Ariel:

  you may try or check how does node.js implement cluster and work_thread model.

Best regards
   tom zhou
Reply all
Reply to author
0 new messages