c - 如何正确终止 pthread?

标签 c multithreading pthreads

我有一个 tcp echo 服务器,它为连接到它的每个客户端创建一个 pthread。对于每个连接,我都有一个递增的变量nbOfClients。 当客户端关闭其连接时,我会检测到它并减少客户端数量。然而,服务器一直认为客户端还活着,并继续尝试从套接字读取/写入。我猜想这是因为创建了客户端的线程导致的,我尝试使用 pthread_cancel 来终止该线程,但没有成功。 我想终止与关闭其连接的某个客户端关联的 pthread。 我该怎么办?

这是我的代码:

static int nbOfClients = 0;

static  pthread_t tid;

int main (int argc, char *argv[]) {

    int bytes_to_read, arg, listen_sd, new_conn, sockfd, client_len, port;
    struct sockaddr_in server, client_addr;
    char *bp, buf[BUFLEN];
    ssize_t n;


    sockfd = 0;

    switch(argc) {
        case 1:
          port = SERVER_TCP_PORT;   // Use the default port
          break;
        case 2:
          port = atoi(argv[1]); // Get user specified port
          break;
        default:
          fprintf(stderr, "Usage: %s [port]\n", argv[0]);
          exit(1);
    }

    // Create a stream socket
    if ((listen_sd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
        error("Cannot Create Socket!");

    // set SO_REUSEADDR so port can be resused imemediately after exit, i.e., after CTRL-c
    arg = 1;
    if (setsockopt (listen_sd, SOL_SOCKET, SO_REUSEADDR, &arg, sizeof(arg)) == -1)
        error("setsockopt");

    // Bind an address to the socket
    bzero((char *)&server, sizeof(server));
    server.sin_family = AF_INET;
    server.sin_port = htons(port);
    server.sin_addr.s_addr = htonl(INADDR_ANY); // Accept connections from any client

    if (bind(listen_sd, (struct sockaddr *)&server, sizeof(server)) == -1)
        error("bind error");

    listen(listen_sd, MAX_CONNECTIONS); ///put a define constant indicating the maximum number of clients #define NB_CLIENTS 3

    while (TRUE) {
        client_len = sizeof(client_addr);
        if ((new_conn = accept(listen_sd, (struct sockaddr *) &client_addr, (socklen_t *)&client_len)) == -1)
          error("accept error");

        if(new_conn > 0) {
            if(nbOfClients < MAX_CONNECTIONS) {
                printf("just here\n");
                printf(">> Initializing remote address: %s\n", inet_ntoa(client_addr.sin_addr));
                nbOfClients++;


                fclose(fp);

                printf("Connections to date: %u \n",nbOfClients);

                printf("make thread\n");
                pthread_create(&tid,NULL,&echo, (void *)new_conn);
                printf("had thread\n");
            }
            else {
                printf("connection limit reached\n");
                if(send(new_conn, "Server full!\n", 13, 0) == -1)
                    perror("send");
                close(new_conn);
            }
        }
    }

    return(0);
}

void * echo(void *arg) {
    char buf[BUFSIZE]; /* message buffer */
    int n, i = 0;

    bzero(buf, BUFSIZE);
    if(send((int)arg, "Welcome!!\n", 20, 0) == -1)
        perror("send");

    detect_closed_connection(arg);

    while(TRUE) {
        n = read((int)arg, buf, BUFSIZE);

        /**read: read input string from the client*/
        if(n < 0) {
            perror("error reading from socket");
        }

        printf("Server received from client, %d bytes: %s\n", n, buf);

        /**write: echo the input string in UPPERCASE back to the client*/

        int len = strlen(buf);
        for(i = 0; buf[i]; i++)
            buf[i] = toupper(buf[i]);

        n = write((int)arg, buf, len);
        if(n < 0) {
            error("ERROR writing to socket");
        }
    }
}

void detect_closed_connection(void * listenSocket) {
    struct pollfd pfd;
    pfd.fd = (int)listenSocket;
    pfd.events = POLLIN | POLLHUP | POLLRDNORM;
    pfd.revents = 0;
    while(pfd.revents == 0) {
        if(poll(&pfd, 1, 100) > 0) {
            // if result > 0, this means that there is either data available on the
            // socket, or the socket has been closed
            char buffer[32];
            if (recv((int)listenSocket, buffer, sizeof(buffer), MSG_PEEK | MSG_DONTWAIT) == 0) {
                // if recv returns zero, that means the connection has been closed:

                nbOfClients--;
                pthread_cancel(tid);

            }
        }
    }
}

谢谢。

最佳答案

您应该检查 read() 在为客户端提供服务的线程中是否返回 0,因为 read() 返回 0 以防对等方(此处为客户端)关闭连接。

此行之后

n = read((int)arg, buf, BUFSIZE);

添加

if (0 == n)
{
  fprintf(stderr, "The client closed the connection.\n");
  break;
}

就在线程函数离开之前,您可以添加语句来减少正在运行的线程数。

<小时/>

另外请注意,nbOfClients 由所有“客户端”线程以及主线程同时访问,因此访问它应受到保护,例如通过使用互斥体。

<小时/>

还有另一个问题,因为在读取缓冲区时对 strlen() 的调用期望缓冲区以 0 终止,这不一定需要是情况下,即使您发送了以 0 结尾的“字符串”。 read() 很可能会返回客户端以多个部分发送的“字符串”。因此,循环 read() 直到收到 0 终止符。

<小时/>

不要通过调用pthread_cancel()使线程自行结束,而是使用pthread_exit()

关于c - 如何正确终止 pthread?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20303812/

相关文章:

c - 内置/内在测试

java - 创建线程时出现 NullPointerException

c - 为什么信号量不受来自不同线程的帖子的影响?

c++ - 线程意外结束。 C++

objective-c - sizeof() 不适用于传递给 GLfloat[] 数组的引用

c - 在多进程环境中读取文件时意外的 EOF

java - 应用程序可能在其主线程(抽屉导航)上做了太多工作

java - 将数据从多个线程发送到单个线程

c - 为什么 printf 不能在 c 中使用多线程?

c - 具有多个客户端的单个服务器