简体   繁体   中英

Client hangs after pthread_exit() in server thread in C

I have a server where I handle multiple clients. Each client that connects to it is serviced in its own thread. Now, if any errors occur on the server side, I want to exit that thread by calling pthread_exit, and terminate the client that was being serviced by that thread. However; when I try to do so, my client is hanging. Also, this causes other clients that are in different threads to hang as well. I called pthread_exit in a random spot to test it...

Most likely the problem is that you are not calling close(newsockfd) before you call pthread_exit(). If so, then your server-thread goes away, but the socket that it was using to communicate with the client remains open, even though the server is no longer doing anything with it. Then the client's outgoing TCP buffer fills up, and the client waits indefinitely for the server to recv() more data from the socket, which never happens.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM