Pthread recv Blocks

This is a discussion on Pthread recv Blocks within the C Programming forums, part of the General Programming Boards category; HI I am new to Posix Threads and I have a few questions. I am writing a server program, there ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    14

    Pthread recv Blocks

    HI I am new to Posix Threads and I have a few questions.

    I am writing a server program, there is a while loop accepting conections. When a connection gets accept()ed a new thread is created. The thread function is in charge of recv()ing the data from the client now everything all right so far. The problem is that recv() blocks I thought threads would solve this problem. Does recv() blocks no matter what? do I have to use select() or any other function that allows me to check for incomming data?


    Thank you very much in advance

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,699
    recv() blocks by default. One reason for using a thread is so recv() can without preventing your main thread from accepting additional connections.

    If you really don't won't your recv() threads from blocking, you can enable non-blocking input on the socket. But using select() is probably better.

    gg

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    14
    So recv blocks no matter what even if it is a separate thread for it.????

    Sorry but im not very clear on this

    Thanks

  4. #4
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    If every connect()ion is handled by a different thread every time, even if it blocks it doesn't really matter. The only problem might be that during the recv() the other party might not respond ever, leaving your thread kind of like a "zombie", there and not there. You could set the timeout parameters of the socket though and check something ... i can't remember now errno perhaps for the fact that the recv timed out, and properly pthread_exit() the thread.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    14

    reply

    so basically what you are saying is that recv() will only block for that thread

    sorry again hopefully this is going to be my last question


    Thanks

  6. #6
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Yes, except if you are using any readily found code that might call pthread_join, you don't want that, every thread will block on it's own recv.
    Every thread has it's own execution point, which the OS manages whether it will run or not.
    A thread might be stopped by the OS, even if it performs a function call.
    In your case, the thread blocks on recv() and logically the OS sends it to sleep.
    Threads share the programs memory space, but every thread has it's own little stack space to keep it's own variables and function calls.
    For a server pthreads are ideal in my opinion because they impose the least overhead while being seriously multi-tasking.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    14

    Thumbs up reply

    perfect. Thanks that cleared up alot of doubts thanks very helpfull

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about recv
    By carrotcake1029 in forum Networking/Device Communication
    Replies: 2
    Last Post: 02-26-2009, 02:10 PM
  2. recv() malfunctioning in a pthread?
    By th3void in forum Networking/Device Communication
    Replies: 7
    Last Post: 11-12-2007, 01:00 PM
  3. The Pthread Hell
    By matott in forum Linux Programming
    Replies: 1
    Last Post: 04-10-2005, 06:59 AM
  4. recv() blocks
    By multielements in forum Networking/Device Communication
    Replies: 6
    Last Post: 12-20-2004, 11:57 AM
  5. recv()
    By afisher in forum Networking/Device Communication
    Replies: 3
    Last Post: 03-24-2004, 05:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21