Need blocking for function
I am using pthreads and sockets. What I am going to be having is multiple socket connections (no more than 5 or so). What I need is a recv() function on each of them in their own thread.
I just basically need to know what a good way is. The way I am doing it now runs my program at like 95% cpu usage. I can't believe this is good, but I don't know another way. Does anyone know how to keep recv blocking?
Below is some code, main is some pseudo code. As you can see, I just throw some while loops down.
int main (void)
serv = connectserver(host, port);
create_recv_thread(serv, serv_recv, &listen_server);
int create_recv_thread(int server, buffer_t *buffer, void *function)
struct recv_args args;
args.server = server;
args.buffer = buffer;
return pthread_create(&recv_thread, NULL, function, &args);
void *listen_server(void *threadarg)
struct recv_args *my_data;
my_data = (struct recv_args*)threadarg;
server = my_data->server;
buffer = my_data->buffer;
result = recv(server, buffer->buffer_data, 2048, 0);
if (result > 0)
printf("%s %d bytes received through socket #%d\n", gettime(), result, server);
else if (result == 0)
printf("%s Connection to socket #%d closed\n", gettime(), server);
printf("%s recv error\n", gettime());