Logo Questions Linux Laravel Mysql Ubuntu Git Menu

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



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).

like image 432
Krab Avatar asked Mar 11 '14 15:03


1 Answers

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.

like image 75
Andrius Bentkus Avatar answered Oct 03 '22 23:10

Andrius Bentkus