Hello!

You should not call anything except `uv_async_send()` from another
thread. Many functions are inserting stuff into loop's linked lists
and the list itself is not thread-safe (not even talking about many
other internals).

`uv_async_send()` indeed writes to one end of the pipe, and libuv is
listening on the other end of the pipe, and interrupts loop and
invokes callback when receives data.

As a way to return data back from uv_work_cb - you could set and
change req.data safely inside worker thread, it'll be available in
uv_work_done_cb.

Cheers,
Fedor.

On Fri, Jan 17, 2014 at 6:19 PM,  <[email protected]> wrote:
> Hi,
>
> I've been curious about how libuv works internally regarding thread
> communication.
>
> This is what I understand:
> There's a single thread running uv_run. If I spawn some work to be done
> in another thread I'm supposed to use uv_queue_work. The way to send
> the results of this work back to the main thread is through
> uv_async_send (which is the only thread-safe function in libuv).
>
> This is what I want to do:
> Start listening on a TCP socket, accept incoming connections in the
> default loop, and wait for data to be available for reading (using
> uv_read_start). When it's available for reading, I'd put the work
> (socket reading) in the queue (with uv_queue_work), and the callback
> for this would run in another thread, reading the data and performing
> some work based on it, which may involve writing data back to this or
> any other socket.
>
> So, my questions related to this are:
> - Is this possible to do? If I call uv_read_start and uv_write in
> another thread, can this cause trouble, because they should have been
> called in the default loop?
>
> - If uv_read_start and uv_write should be called in the default loop,
> then the main thread will be responsible for reading and writing all
> data, right? Wouldn't this impact performance? How does NodeJS deal
> with this?
>
> - How does uv_async_send work? From a quick glance at the source code,
> I suspect it sends data in a file descriptor, which will be read in the
> default loop. Is this what is happening?
>
>
> Allan
>
> --
> 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 [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/libuv.
> For more options, visit https://groups.google.com/groups/opt_out.

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/libuv.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to