Hello, I'm not quite sure if it's possible to do what I'm about to ask so I thought I'd ask.
I have a multi-threaded program where threads share a memory block to communicate necessary information. One of the information is termination of threads where threads constantly check for this value and when the value is changed, they know it's time for pthread_exit()
. One of the threads contains listen()
function and it seems to wait indefinitely. This can be problematic if there are nobody who wants to make connection and the thread needs to exit but it can't check the value whether thread needs to terminate or not since it's stuck on listen()
and can't move beyond.
while(1)
{
listen();
...
if(value == 1)
pthread_exit(NULL);
}
My logic is something like that if it helps illustrate my point better. What I thought would solve the problem is to allow listen() to wait for a duration of time and if nothing happens, it moves on to next statement. Unfortunately, none of two args of listen()
involves time limit. I'm not even sure if I'm going about the right way with multi-threaded programming, I'm not much experienced at all. So is this a good approach? Perhaps there is a better way to go about it? Thanks for any insightful comments.
EDIT: just to clarify a little, the reason listen()
is in the while loop is that this is a server and will be connected to multiple clients at a time.