On 09/21/2016 06:31 AM, Sunny wrote:
> Hi everyone, I'm working on a simple network application and I'm new to
> the libuv, so I have this noob question to ask
> 
> If I have an application which use only one loop, with many tcp/udp
> handler registered on it, is it safe to use static buffer in uv_alloc_cb
> like this?
> 
> static char buffer[65535] = {0};
> 
> void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t
> *buf) {
>   buf->base = (char*)buffer;
>   buf->len = 65535;
> }
> 
> In my imagine, the event loop of libuv would work in a SINGLE thread
> with the following way:
> 1. System polled an event
> 2. Invoke uv_alloc_cb
> 3. Invoke uv_recv_cb with the allocated buffer and other infomation
> 4. After #3 ends, back to #1
> 
> In this circuit, it's easy to point out that the allocated memory will
> be only used in uv_recv_cb and will not been overwritten before #3 ends,
> so in this situation I think use a static buffer here is possible, but I
> don't know whether the process I imagine is correct.
> 
> If my imagine is correct, is it safe to use the static buffer?
> 

Yes, as long as you don't use Windows.  There the order is not
necessarily maintained for different streams.  You could get
uv_alloc_cb1 uv_alloc_cb2 uv_recv_cb1, uv_recv_cb2.

> More questions if it's safe:
> 1. Does libuv has same behaviour on different platform?

Oops, I replied above.

> 2. Will thread-based handler (file or something else) change the behaviour?
> 

uv_fs_* operations may happen in parallel, since they hapen in a
threadpool, so you'll have to be careful there.


Cheers,

-- 
Saúl Ibarra Corretgé
bettercallsaghul.com

-- 
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 libuv+unsubscr...@googlegroups.com.
To post to this group, send email to libuv@googlegroups.com.
Visit this group at https://groups.google.com/group/libuv.
For more options, visit https://groups.google.com/d/optout.

Reply via email to