如何加入从运行pthread数组结束的pthread

时间:2021-05-04 20:41:54

I have a chat server written in c++ and i have a problem with phtreads. This server suppose to handle multiple connections so i created array of pthread. when a new client connects it gives one pthread to this client from the array and it calls handle function where is comunication inplemented. but when this client disconect i dont know how to find which pthread ended and which index to a pthread array is empty. i know this code is horrible but i really want to make this program better

我有一个用c ++编写的聊天服务器,我有phtreads的问题。这个服务器假设处理多个连接,所以我创建了pthread数组。当一个新的客户端连接时,它从数组中给这个客户端一个pthread并调用handle函数,其中comunication inplemented。但是当这个客户端断开时,我不知道如何找到哪个pthread结束以及哪个pthread数组的索引为空。我知道这段代码很糟糕,但我真的想让这个程序变得更好

so my question is that can i do it this way that when one pthread ends i will empty that index to array of pthread for potencial next client conected in and when more that lets say 100 people connect i will realloc the array ?

所以我的问题是,我可以这样做,当一个pthread结束时,我会将该索引清空为pthread数组,用于潜在的下一个客户端连接并且当更多可以说100个人连接时我将重新分配数组?

how do i find which pthread ended for joining that pthread and for finding which index is now free to use again ?

我如何找到哪个pthread结束加入该pthread并找到哪个索引现在可以再次使用?

struct parsing{
     int socket;
};

int i = 0;
int strop = 100;
pthread_t *thread;
thread = (pthread_t*)malloc(sizeof(pthread_t)*100);
struct parsing args;
while (1)
{
    if((client_sock = accept(socket_desc, (struct sockaddr *)&socket_addr, (socklen_t *)&client_sock_len)) < 0)
    {
        fprintf(stderr,"Accept Error");
        return 5;
    }
    printf("connected %d",client_sock);
    args.socket = client_sock;
    pthread_create(&thread[i], NULL, &handle_connection, (void *)&args);
    i++;
    if (i == strop)
    {
        strop = strop + 100;
        void * ptr = (pthread_t*)realloc(thread,sizeof(pthread_t)*strop);
    }
}   

1 个解决方案

#1


0  

how do i find which pthread ended for joining that pthread and for finding which index is now free to use again ?

我如何找到哪个pthread结束加入该pthread并找到哪个索引现在可以再次使用?

The pthreads API does not provide a way to discover terminated threads. Glibc has has a non-portable extension by which you can attempt a non-blocking join of a specific thread (see pthread_tryjoin_np()), and if that's available to you then you could build a polling-based solution around it.

pthreads API不提供发现已终止线程的方法。 Glibc有一个非可移植的扩展,您可以通过它来尝试特定线程的非阻塞连接(请参阅pthread_tryjoin_np()),如果您可以使用它,那么您可以围绕它构建基于轮询的解决方案。

But I'd say the right way to accomplish this is to establish a mechanism for worker threads to tell the main thread that they are done. For instance, they might add their own thread ID to a linked list that the main thread can also see, and then send a signal or use pthread_cond_signal() to get the main thread's attention. Do be sure to provide proper synchronization for multi-threaded access to shared variables.

但我要说的是,实现这一目标的正确方法是为工作线程建立一种机制,告诉主线程它们已完成。例如,他们可能会将自己的线程ID添加到主线程也可以看到的链表中,然后发送信号或使用pthread_cond_signal()来获取主线程的注意。确保为多线程访问共享变量提供适当的同步。

#1


0  

how do i find which pthread ended for joining that pthread and for finding which index is now free to use again ?

我如何找到哪个pthread结束加入该pthread并找到哪个索引现在可以再次使用?

The pthreads API does not provide a way to discover terminated threads. Glibc has has a non-portable extension by which you can attempt a non-blocking join of a specific thread (see pthread_tryjoin_np()), and if that's available to you then you could build a polling-based solution around it.

pthreads API不提供发现已终止线程的方法。 Glibc有一个非可移植的扩展,您可以通过它来尝试特定线程的非阻塞连接(请参阅pthread_tryjoin_np()),如果您可以使用它,那么您可以围绕它构建基于轮询的解决方案。

But I'd say the right way to accomplish this is to establish a mechanism for worker threads to tell the main thread that they are done. For instance, they might add their own thread ID to a linked list that the main thread can also see, and then send a signal or use pthread_cond_signal() to get the main thread's attention. Do be sure to provide proper synchronization for multi-threaded access to shared variables.

但我要说的是,实现这一目标的正确方法是为工作线程建立一种机制,告诉主线程它们已完成。例如,他们可能会将自己的线程ID添加到主线程也可以看到的链表中,然后发送信号或使用pthread_cond_signal()来获取主线程的注意。确保为多线程访问共享变量提供适当的同步。