I am expecting to get 100,000 requests at least 100 of them would be concurrent. Each time I get a request, I am creating a new thread and destroy it once it's done using pthread_exit()
. Using pthread_detach
I am getting 99% success rate. Is there a better way than this?
pthread_t hilo;
// infinite loop
while ((client_sock = accept(server_sock, (struct sockaddr *) &client_sockaddr, &len))) {
struct ClientSocket socks;
// some code...
pthread_create(&hilo, NULL, func, &socks);
pthread_detach(hilo);
printf("\nSocket is listening for the next request...\n");
}
I heard pthread_join
would be a better way to utilize resources without reaching the thread limit, but the way I am doing it is not concurrent.
pthread_t hilo;
// infinite loop
while ((client_sock = accept(server_sock, (struct sockaddr *) &client_sockaddr, &len))) {
struct ClientSocket socks;
// some code...
pthread_create(&hilo, NULL, func, &socks);
pthread_join(hilo, NULL); // it stops the main thread
printf("\nSocket is listening for the next request...\n");
}
Any ideas would be appreciated!