libuv - how to stop tcp server, which runs in another thread
Asked Answered
N

1

6

For example i have 2 threads. I want to stop server from main thread (Thread 1).

Thread 1: main program

Thread 2: TcpServer

From libuv library:

/*
 * 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*);

That means, if i would call uv_stop(tcp_server_loop) in main thread and the server loop will be blocked because of no events on the tcpserver, then the server will be still in the loop till some event appears. (it probably checks if uv_stop was called before the loop goes to the block mode to wait for new events).

Nyssa answered 11/3, 2014 at 15:23 Comment(0)
S
6

If you run uv_run with UV_RUN_DEFAULT, it will be a blocking call. However, if you use uv_stop then uv_run will immediately return. Remember that the only function in uv that is thread safe is uv_async_send, so if you want to call uv_stop on your TcpServer loop, you will have it to do from within the loop.

Specious answered 26/5, 2014 at 8:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.