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

后端 未结 7 1817
暗喜
暗喜 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:45

    Do not block on recv. Rather implement a block on 'select' and test the input fdset to determine if there is anything to read in the first place.

    If there is data to be read, call recv. If not, loop, check 'isrunning' volitile Boolean variable that gets set by the other thread when shutdown has been initiated.

提交回复
热议问题