On Tue, Aug 11, 2020 at 1:42 PM Jason Heeris <jason.hee...@gmail.com> wrote:
>
> I'm developing a control system that uses libuv (1.34.2) on a GNU/Linux 
> system. My control system basically has a mutex-protected in-memory message 
> queue, posts my custom messages to it, and calls async_send(). The handler is 
> an async handler run by the main loop.
>
> You could think of each message as more or less corresponding to single, 
> non-blocking function to be called in the main loop. So the handler pops a 
> message off, uses it to decide what non-blocking function to call, and calls 
> it.
>
> The problem I want to solve is that if I get a bunch of messages all at once 
> in the queue (eg. after libuv coalesces async_send calls, or my control code 
> posts multiple messages at once), I (a) don't want to monopolise the mutex 
> and (b) don't want to monopolise the main loop. (a) is solved by popping the 
> messages off the queue, storing them, releasing the mutex and then processing 
> the messages outside the mutex lock. But (b) is a bit harder. I want to let 
> libuv handle other things in between the function call for each message (if 
> it needs to).
>
> Is there a way to give libuv a function to run once, queued up in the main 
> loop? I couldn't find any API functions that simply add a one-off function 
> call to the loop.

There isn't*, but if you want to slice the work into smaller pieces,
you can use e.g. a uv_timer_t, uv_idle_t or uv_async_t to split it up.

You'll have to think about queuing, of course - what if items come in
faster than the main thread can dispatch them?

* Or rather, there is - you could use the uv_after_work_cb to
uv_queue_work() for that - but it'd be rather inefficient.

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/libuv/CAHQurc9jgr4PpyuN8QpDrRQfK7DJmuh0shdCUhfFuAfruiypYg%40mail.gmail.com.

Reply via email to