6

例如我有2 个线程。我想从主线程(线程 1)停止服务器。

线程1:主程序

线程 2:TcpServer

来自 libuv 库:

/*
 * This function will stop the event loop by forcing uv_run to end
 * as soon as possible, but not sooner than the next loop iteration.
 * If this function was called before blocking for i/o, the loop won't
 * block for i/o on this iteration.
 */
UV_EXTERN void uv_stop(uv_loop_t*);

这意味着,如果我uv_stop(tcp_server_loop)在主线程中调用并且服务器循环将因为 tcpserver 上没有事件而被阻塞,那么服务器将仍然在循环中,直到某个事件出现。uv_stop(它可能会在循环进入块模式以等待新事件之前检查是否被调用)。

4

1 回答 1

6

如果您使用 UV_RUN_DEFAULT 运行 uv_run,这将是一个阻塞调用。但是,如果您使用 uv_stop 则 uv_run 将立即返回。记住 uv 中唯一线程安全的函数是 uv_async_send,所以如果你想在你的 TcpServer 循环上调用 uv_stop,你可以在循环内进行。

于 2014-05-26T08:54:20.673 回答