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
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.
So recv blocks no matter what even if it is a separate thread for it.????
Sorry but im not very clear on this
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.
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
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.
perfect. Thanks that cleared up alot of doubts thanks very helpfull