How to let a thread which blocks on recv() exit gracefully?

后端 未结 7 1845
暗喜
暗喜 2020-12-11 17:27

There is a thread likes this:

{  
    ......    
    while (1)
    {
        recv(socket, buffer, sizeof(buffer), 0);
        ......
    }
    close(socket         


        
7条回答
  •  余生分开走
    2020-12-11 17:47

    Declare some 'stop' boolean, check it after every recv() return and terminate if it's set. To shut down, set the bool and close the socket from another thread. The blocking recv() will return 'immediately' with an error, but it does not matter 'cos you're about to terminate anyway:)

提交回复
热议问题