나는 C++로 작성된 채팅 서버를 가지고 있으며 phtreads에 문제가 있습니다. 이 서버는 pthread의 배열을 만들도록 여러 개의 연결을 처리한다고 가정합니다. 새로운 클라이언트가 연결되면 배열로부터이 클라이언트에 하나의 pthread를주고 처리 함수를 호출합니다. 여기서 함수는 구현 된 것입니다. 하지만이 클라이언트 disconect 때 pthread가 끝나고 pthread 배열에 어떤 인덱스가 비어 있는지 찾는 방법을 모른다. 나는이 코드가 끔찍한 것을 알고 있지만이 프로그램을 더 잘 만들고 싶다.실행중인 pthread 배열에서 끝난 pthread에 가입하는 방법
그래서 내 질문은 내가 할 수있는 한 가지 pthread가 끝날 때 그 다음에 잠재 고객을위한 pthread의 배열에 그 색인을 비울 것이다. 그리고 더 많은 사람들이 100 명의 사람들이 내가 어레이를 realloc 연결한다고 말할 수 있을까요?
어떻게 그 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);
}
}
std :: thread를 사용하십시오. –