Thread: Need blocking for function

  1. #1
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008

    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)
        create variables...
        serv = connectserver(host, port);
        if (serv)
            create_recv_thread(serv, serv_recv, &listen_server);
    int create_recv_thread(int server, buffer_t *buffer, void *function)
    	pthread_t recv_thread;
    	struct recv_args args;
    	args.server = server;
    	args.buffer = buffer;
    	return pthread_create(&recv_thread, NULL, function, &args);
    void *listen_server(void *threadarg)
    	int result;
    	struct recv_args *my_data;
    	int server;
    	buffer_t *buffer;
    	my_data = (struct recv_args*)threadarg;
    	server = my_data->server;
    	buffer = my_data->buffer;
    	while (1)
    		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());

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Portland, OR
    Your problem is the "while(1);" in your main thread.
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);

  3. #3
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    But I don't want the program to end. I need to keep it open because with some data I receive it will trigger my program to send data. I want to keep the connection alive until I choose to kill it.

  4. #4
    Registered User
    Join Date
    Apr 2008
    But I don't want the program to end.
    Then use appropriate functions: pthread_join() for instance instead of while(1). Besides, recv() is blocking by default except if you switched your socket streams to non-blocking which doesn't seem to be the case.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to intilise a non blocking socket?
    By lolguy in forum Networking/Device Communication
    Replies: 7
    Last Post: 03-20-2009, 12:18 AM
  2. non blocking operation
    By Alon in forum C++ Programming
    Replies: 4
    Last Post: 02-17-2009, 11:03 AM
  3. How to initialize a non blocking socket using only winsock library
    By *DEAD* in forum Networking/Device Communication
    Replies: 4
    Last Post: 01-18-2008, 07:03 AM
  4. Socket Blocking Trouble!
    By LearningMan in forum Windows Programming
    Replies: 6
    Last Post: 01-09-2003, 10:09 AM
  5. Non blocking listen() with winsock
    By manwhoonlyeats in forum C Programming
    Replies: 1
    Last Post: 12-08-2002, 07:00 PM