我的理解是,正确使用uv_async
一次使用如下:
uv_async_t
句柄;uv_async_init
;uv_async_send
来调度回调;uv_close
注销句柄;uv_async_t
句柄;例如:
uv_async_t *handle = (uv_async_t*)malloc(sizeof(uv_async_t));
uv_async_init(&uvLoop, handle, [](uv_async_t *handle) {
// My async callback here
uv_close((uv_handle_t*)handle, [](uv_handle_t* handle) {
free(handle);
});
});
uv_async_send(&asyncCb->uvAsync);
根据我收集的信息,uv_close
在uvLoop中被异步调用。因此,我想做以下工作,以避免在事件循环中排队进行两个回调:
uv_async_t *handle = (uv_async_t*)malloc(sizeof(uv_async_t));
uv_async_init(&uvLoop, handle, nullptr);
uv_close((uv_handle_t*)handle, [](uv_handle_t* handle) {
// My async callback here
free(handle);
});
还有人这样做吗,这被认为是安全的吗?
发布于 2017-01-06 07:19:38
你的目标是什么?需要使用多个线程吗?如果是这样的话,那就行不通了,因为uv_close
并不是线程安全的。
如果您只想在循环中调度将来的回调,请检查uv_idle_t
。您还可以使用队列并根据需要启动/停止句柄,而不是随后创建和销毁句柄。
https://stackoverflow.com/questions/41499152
复制相似问题